最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How (e.g. in what order) does this code run and what does it do? - Stack Overflow

programmeradmin1浏览0评论

I have found this crazy javascript code.

Can someone elaborate the exact steps this code is going through and why?

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

I have found this crazy javascript code.

Can someone elaborate the exact steps this code is going through and why?

(function a(a){
    return a;
})
(function b(b){
    return b;
})
(function c(c){
    return c;
})
(true);
Share Improve this question edited Sep 5, 2013 at 6:50 Nanne 64.4k16 gold badges122 silver badges166 bronze badges asked Sep 5, 2013 at 4:59 JoonJoon 9,8848 gold badges50 silver badges75 bronze badges 3
  • 5 Its just three functions and returning just true.. Thats it – suresh gopal Commented Sep 5, 2013 at 5:05
  • I'm not. My friend gave me as a puzzle. – Joon Commented Sep 5, 2013 at 5:06
  • 2 The tricky part of this "puzzle" are not the automatically invoked functions, but why they are called from left to right. If you add a console.log statement before every return, you will see that a gets outputted before b, which gets outputted before c. But how can function a know the input arg before function b returns it? – Steve Commented Sep 5, 2013 at 5:25
Add a comment  | 

6 Answers 6

Reset to default 10
  • This will self-invoke a which is given function b as argument (since a is defined as a variable inside a's local scope that will take presence over the function a which is declared in parent scope.).
  • Then it will self-invoke b which is given c as as argument.
  • Finally function c is self-invoked which returns true as that is given as argument.

You can look at it as a chain doing this:

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 when inside the function (local scope) is a variable because function foo(bar){} is the same as function(){var bar = arguments[0]}.

The function a could be written like this and do the same exact thing:

function a(foo){
    return foo;
}

You can verify by doing this:

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

Console output (updated to show in FF as well use Chrome to see the function definition output):

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

To understand what's going on, simplify it:

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

The above code, run in the console, will output 10. What's happening is the brackets are telling the code to run immediately (it's called a self-invoking function), taking whatever follows as a parameter. So I'm creating function a, and immediately running it with 2 as the parameter.

The code you have is basically the same thing, but with more levels and no multiplication. All of your functions just return their parameters, and the one being passed is the boolean true, so the code will output true at the end.

This returns true.

First function a (bad name?) is defined which takes an argument and returns it. This function has been called immediately, with an argument which is return value of (function b(b){ return b; })(function c(c){ return c; })(true). Which gets evaluated before the a gets called.

(function b(b){ return b; })(function c(c){ return c; })(true) is a similar construct, with a function b being defined which returns the argument it receives, and again is called immediately with similar argument and same for the third function c.

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

Its just return the next part of parameter with () symbol.

I'll take a stab at it.

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

These are all self-invoking functions.

But the last one:

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

Gets the value true passed in. So, when it returns "c" - you get true.

For the others, it just moves up as anonymous functions get passed in the value return by the function. So, a visual.

   (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.

The functions are executed in a top-down order. You can call a function with another function as its argument. This is fine. It keeps doing this until "true" is passed as an argument, in which case the calling chain returns "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); 
发布评论

评论列表(0)

  1. 暂无评论