简体   繁体   中英

Javascript function construct

What does this construct mean?

var digits = [1, 2, 3];

(function fetchData(name){
 //body
})(digits.shift());

which construct?

I can explain what's happening, if that will help:

(in the order of execution)

  1. array gets created and assigned the name of "digits"
  2. the first element of "digits" gets removed from the array - so the length of the array gets reduced
  3. that value that got removed gets passed as an argument into the function fetchData() as the variable "name"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM