user80805
user80805

Reputation: 6508

Javascript function construct

What does this construct mean?

var digits = [1, 2, 3];

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

Upvotes: 1

Views: 357

Answers (3)

Chris Cherry
Chris Cherry

Reputation: 28554

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.

Upvotes: 2

Zimbabao
Zimbabao

Reputation: 8240

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.

Upvotes: 1

arnorhs
arnorhs

Reputation: 10429

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"

Upvotes: 2

Related Questions