Javascript functions: var func = someFunction() and var func = someFunction? -
var funchi = function() { var a=6; var b=5; return a+b; }; var withbrace = funchi(); var withoutbrace = funchi; console.log("with braces: "+withbrace) //'reference 1' console.log("without braces: "+withoutbrace) //'reference 2' console.log("without braces: "+withoutbrace()) //'reference 3'
the code pretty simple , clear. 'reference 1' , 'reference 3', console show 11 i'm not clear use 'reference 2'. 'refernce 2', console show complete function instead of showing 11. many times, use 'reference 2' thing (e.g. window.onload = initall) how useful.
window.onload = initall; //what do? why not 'window.onload = initall()'
i not clear concept behind it. if possible, give me link lesson on thing?
in first case: withbrace
contains result of calling funchi
, therefore it's 11
in second case: withoutbrace
references function funchi
. therefore withoutbrace === funchi
, can withoutbrace
function, same function funchi
. can call function funchi
via withoutbrace
doing withoutbrace()
, 11
- which third case.
var funchi = function() { var a=6; var b=5; return a+b; }; //assigning result of calling funchi withbrace var withbrace = funchi(); typeof funchi; //function typeof withbrace; //number withbrace === 11 //true, withbrace 11, result of calling funchi //assigning funchi withoutbrace var withoutbrace = funchi; typeof funchi; //function typeof withoutbrace; //function withoutbrace === funchi; //true, same function funchi(); //11 withoutbrace(); //11, return same thing since withoutbrace points funchi
Comments
Post a Comment