What does this construct mean?
var digits = [1, 2, 3];
(function fetchData(name){
//body
})(digits.shift());
asked Mar 12, 2011 at 6:48
user80805
6,5685 gold badges30 silver badges31 bronze badges
3 Answers 3
That syntax is used to prevent identifiers from leaking into the containing namespace.
After that code, if you attempt to call fetchData(name) you will find that fetchData is undefined, thanks to the () wrapper.
answered Mar 12, 2011 at 6:54
Chris Cherry
28.6k6 gold badges73 silver badges72 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
which construct?
I can explain what's happening, if that will help:
(in the order of execution)
- array gets created and assigned the name of "digits"
- the first element of "digits" gets removed from the array - so the length of the array gets reduced
- that value that got removed gets passed as an argument into the function fetchData() as the variable "name"
answered Mar 12, 2011 at 6:53
arnorhs
10.4k2 gold badges37 silver badges38 bronze badges
Comments
Declare a function and call it.
Click on this http://jsfiddle.net/TC8K6/ you will see a alert of the parameter passed to the function.
answered Mar 12, 2011 at 6:54
Zimbabao
8,2803 gold badges32 silver badges36 bronze badges
Comments
lang-js