JavaScript advanced function call -
i learning javascript , don't understand style of writting js code read. know "old" way didn't found manual explained style of create , calling js function. "new way":
// object var dog = { color: 'brown', size: 'big' }; //function write dog size (function(dog) { return dog.size; })(dog); from javascript manual know (old way):
function prinsize(dog) { return dog.size } console.log(prinsize(dog)); please send me link js doc, explained why
in js code on start , end of function brackets
why possible after declare of function write brackets .(dog) , call function argument dog.
to understand better, consider alternative way write number 1:
(function() { return 1; })() what define function returns 1, executes () following it, whole thing evaluates number 1.
of course, there no reason this, might write 1.
so 1+2, write
(function() { return 1; })() + function() { return 1; }() (i don't need parentheses around second function, since in position syntactically expression.)
in same sense, function
(function(dog) { return dog.size; })(dog) is exactly equivalent writing dog.size start with. don't know saw this, standing on own, ridiculous in 2 ways: first of all, said same dog.size, , second, not resulting size--to result, you'd need write
var size = function(dog) { return dog.size; }(); but again, might write
var size = dog.size; there plenty of reasons use these so-called "iifes", or immediately-invoked function expressions, including hiding information inside them, not 1 of them.
Comments
Post a Comment