Why does a Javascript function act differently upon instantiation than execution?
问题 I'm coming from C#/PHP and trying to get my head around Javascript's idea that functions are variables/objects and have quasi constructors, etc. Can anyone explain why the following code functions as it does, namely: Why isn't "2" displayed when instantiating the variable/function test ? Why isn't "1" displayed when executing the variable/function test ? code: var setup = function () { console.log(1); return function() { console.log(2); }; }; var test = setup(); // 1 test(); // 2 test(); // 2