这段代码的运行方式(例如以什么顺序)以及它的作用是什么?

时间:2013-09-05 04:59:40

标签: javascript

我发现了这个疯狂的javascript代码。

有人可以详细说明这段代码的确切步骤以及原因吗?

(function a(a){
    return a;
})
(function b(b){
    return b;
})
(function c(c){
    return c;
})
(true);

6 个答案:

答案 0 :(得分:10)

  • 这将自我调用a作为参数function b(因为a被定义为{{>>变量 a 1}}的本地范围将占据在父范围内声明的函数 a。)。
  • 然后它会自我调用b作为参数给出c
  • 最后,函数c是自我调用的,返回true,因为它是作为参数给出的。

您可以将其视为链条:

a(var a)    // function b given as arg. When a returns b() will be invoked
   b(var b) // function c given as arg. When b returns c() will be invoked
      c(true)

a在函数内部(本地范围)是变量,因为function foo(bar){}function(){var bar = arguments[0]}相同。

函数a可以像这样编写并执行相同的操作:

function a(foo){
    return foo;
}

您可以通过执行以下操作进行验证:

console.log('start');

(function a(a){
    console.log('a', typeof a);
    return a;
})
(function b(b){
    console.log('b', typeof b);
    return b;
})
(function c(c){
    console.log('c', typeof c);
    return c;
})
(true);

console.log('end');

<强> ONLINE FIDDLE HERE

控制台输出(更新显示在FF以及使用Chrome查看功能定义输出):

> start
> a function
> b function
> c boolean
> end

答案 1 :(得分:4)

要了解发生了什么,请简化它:

(function a(d){
    return 5*d;
})
(2)

在控制台中运行的上述代码将输出10。正在发生的是括号告诉代码立即运行(它被称为自调用函数),将后续内容作为参数。所以我正在创建function a,并立即以2作为参数运行它。

你拥有的代码基本上是相同的,但有更多的级别,没有乘法。所有函数都只返回它们的参数,传递的函数是布尔值true,因此代码最后会输出true

答案 2 :(得分:2)

返回true

首先定义function a(错误名称?),它接受一个参数并返回它。此函数已立即调用,其参数为(function b(b){ return b; })(function c(c){ return c; })(true)的返回值。在a被调用之前会对其进行评估。

(function b(b){ return b; })(function c(c){ return c; })(true)是一个类似的构造,定义了一个函数b,它返回它接收的参数,并且再次使用类似的参数立即调用,第三个函数c也是如此。

答案 3 :(得分:1)

这些功能以自上而下的顺序执行。您可以使用另一个函数作为参数调用函数。这可以。它一直这样做,直到“true”作为参数传递,在这种情况下,调用链返回“true”。

(function a(a){       // Function a is called with function b as an argument.
    console.log('a'); // The console.log statement is executed,
    return a;         // Then function a returns b 
})                    

(function b(b){       // Function b is called with function c as an argument.
    console.log('b')  // The console.log statement is executed
    return b;         // And then function b returns c 
})

(function c(c){       // Function c is called with "true" as an argument
    console.log('c')  // The console.log statement is executed
    return c;         // And then function c returns true.
})

(true); 

答案 4 :(得分:1)

(function a(a){
            alert(a);
        return a;
    })
    (function b(b){
        alert(b);
        return b;
    })
    (function c(c){
        alert(c);
        return c;
    })
    (true);

它只是用()符号返回参数的下一部分。

答案 5 :(得分:1)

我会捅它。

(function a(a){
  return a;
})
(function b(b){
 return b;
})
(function c(c){
return c;
})
(true);

这些都是自我调用函数。

但是最后一个:

    (function c(c){
      return c;
    })
    (true);

获取传入的值true。因此,当它返回“c”时 - 你会得到真的。

对于其他人,它只是向上移动,因为匿名函数在函数返回的值中传递。所以,一个视觉。

   (function a(a){   <-- the return of b, gets passed in here
      return a;
    })(function b(b){return b;}) <-- the return of c, gets passed in here

    (function c(c){return c;})(true);  <--- true gets passed into c.