简体   繁体   English

匿名函数如何传递参数

[英]How anonymous functions are passing arguments

I've been playing around with javascript functions and I stuck understang the flow of a piece of code: Let's take this example 我一直在使用javascript函数,并且使我难以理解一段代码的流程:让我们来看这个示例

Test = (function(arg) {
    function Test(str) {
        console.log(arg + ' ' + str);
    }

    console.log('toto');

    return Test;
})()

So, if I run this I'll get 'toto', fine ! 所以,如果我运行此命令,我会得到“ toto”,很好! Now If do this: 现在,如果这样做:

Test = (function(arg) {
        function Test(str) {
            console.log(arg + ' ' + str);
        }

        console.log('toto');

        return Test;
    })('titi')
Test('tata');

I'll get: 我去拿:

toto
titi tata

How is this happening, when I wrote Test('tata'), did I call the first Test, I mean: 当我编写Test('tata')时,这是怎么回事,我叫第一个Test是什么意思?
Test = (function(arg) ... 测试=(function(arg)...
Or I ran the function Test(str) ? 还是我运行了Test(str)函数?
And If I'm actually running the first Test, How it passed Tata to the function Test inside of it? 如果我实际上正在运行第一个Test,它将如何将Tata传递给它内部的Test函数?
And finally, Why it didn't log toto another time when I called Test('tata')? 最后,为什么当我调用Test('tata')时却没有登录到另一个时间?

Your anonymous function returns the inner function "Test". 您的匿名函数返回内部函数“ Test”。 The console.log("toto") call is not inside that function. console.log("toto")调用不在该函数内。

Calling the returned function does not call the anonymous function again. 调用返回的函数不会再次调用匿名函数。 Your inner function however retains it's "memory" of the "arg" value passed in, and that explains why it logs "titi tata". 但是,您的内部函数保留了传入的“ arg”值的“内存”,这解释了为什么记录“ titi tata”的原因。

So, what happens step-by-step is: 因此,逐步发生的是:

  1. The variable "Test" is declared; 声明变量“ Test”;
  2. The anonymous function is called. 匿名函数被调用。
  3. The inner function "Test" is defined in the local context of the anonymous function. 内部函数“ Test”是在匿名函数的本地上下文中定义的。
  4. The "toto" log statement is executed. 执行“ toto”日志语句。
  5. The return statement is executed, and the function exits with a reference to the inner function "Test". 执行return语句,该函数以对内部函数“ Test”的引用退出。
  6. The return value is assigned to the variable "Test' in the outer context. 返回值在外部上下文中分配给变量“ Test”。
  7. The returned function is called via the reference in the outer "Test" variable, with the argument being the string "tata". 通过外部“ Test”变量中的引用调用返回的函数,参数为字符串“ tata”。
  8. The console.log() statement in the returned function is run, using a string constructed by concatenating the value of "arg" as it stood when the function was created ("titi") with the argument passed in ("tata"). 运行一个返回字符串中的console.log()语句,它使用一个字符串,该字符串通过将函数创建时的“ arg”值(“ titi”)与传入的参数(“ tata”)连接起来而构成。

So the function returned in step 5 "remembers" the "titi" argument, so any call to that function will result in the string "titi" being prepended. 因此,在步骤5中返回的函数会“记住”“ titi”参数,因此对该函数的任何调用都将导致字符串“ titi”被添加。

The inner Test is a new function, unrelated to the initial Test . 内部Test是一个新功能,与初始Test无关。 Further, you've returned it, however it's maintained the arg with it's value of titi within its closure. 进一步,您已经返回了它,但是它在其闭包内保持了其titi值的arg

When you assign Test , it outputs the "toto", but never runs the inner Test function, so you do not see "titi" until you call your Test('tata') which then calls the inner returned function which has saved "arg" as "titi" initially. 当您分配Test ,它将输出“ toto”,但从不运行内部Test函数,因此您不会看到“ titi”,直到调用Test('tata') ,然后调用内部已保存的函数“ arg最初是“ titi”。

If you want to think about how it's run, you would actually end up with this (where Test is assigned the inner function, and has saved 'titi' as arg ): 如果您想考虑它的运行方式,那么您实际上会以这种方式结束(在Test中分配了内部函数,并将'titi'保存为arg ):

Test = function(str) {
  console.log('titi' + ' ' + str);
}
console.log('toto');

Test('tata');

Let's analyze your code a bit: 让我们来分析一下您的代码:

Full code (slightly edited): 完整代码(略作编辑):

TestO = (function(arg) {
    function TestI(str) {
        console.log(arg + ' ' + str);
    }

    console.log('toto');

    return TestI;
})()

The outer part: 外部:

TestO = function(arg) { ... } (/* 0 parameters */)

Test is initialized to the return value of that anonymous function called with 0 parameters. 测试被初始化为使用0参数调用的匿名函数的返回值。

The inner function: 内部功能:

function TestI(str) {
    console.log(arg + ' ' + str); outer function
}

arg is the value of the argument passed to the outer function. arg是传递给外部函数的参数的值。 It is available because of closures. 由于关闭而可用。

Outer function body 外功能体

console.log('toto'); 
return TestI; // this value will be passed to TestO

This code is executed when you call the anonymous outer function, not TestO, not TestI. 当您调用匿名外部函数而不是TestO而不是TestI时,将执行此代码。 The outer function is called when TestO is initialized. 初始化TestO时,将调用外部函数。

Conclusion 结论

The anonymous outer function is called when TestO is initialized, logs 'toto' and initializes TestO with the TestI function (which is also a closure over the arg parameter of the anonymous outer function). 初始化TestO时,将调用匿名外部函数,将其记录为“ toto”并使用TestI函数初始化TestO(这也是匿名外部函数的arg参数的闭包)。

TestO('tata') // calls TestI with the closured value of `arg` as `arg` and 'tata' as `str`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM