Forum Moderators: open

Message Too Old, No Replies

explain code

         

lichul

12:09 pm on Feb 7, 2011 (gmt 0)

10+ Year Member



can any one explain about what the mean of this javascript code?


(function(param1, param2){
//code
})(name);


thank u before...

Fotiman

1:42 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
The following thread explains that this is a self instantiating anonymous function:
[webmasterworld.com...]

One thing not mentioned in that thread is that it's also a convention that when you have a self instantiating function you wrap it in parenthesis. The reason for it is so that you know right at the beginning that this is a self instantiating function (vs. just another function definition).

Hope that helps.

lichul

8:27 am on Feb 8, 2011 (gmt 0)

10+ Year Member



thank u so much...
but i still not really understand... :D
what is the different about "self instantiating function" and "just another function definition" ?

and else, i'm looking for some article that explain about this snippet, but i can't found. if you have have some resource/article about this, i'll be very happy if you want to share... :)
Thank u...

Fotiman

3:18 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, I thought it was explained fairly well in that link, but I'll break it down (again). :)

In JavaScript you can create "anonymous" functions (functions that are defined and called without being bound to an identifier).


function foo() {
}

That is NOT an anonymous function because it is bound to the identifier "foo". That function could be executed some point later in the code:.


function () {
}

That IS an anonymous function, as it's not bound to an identifier and therefore there is no way to execute it later in the code. Anonymous functions are sometimes used for assigning event handlers:

someelement.onclick = function () {};

An anonymous function can also be executed immediately. To call a named function (like foo in the earlier example), you place parenthesis after the reference name (with any parameters included in the parenthesis). For example:


foo();


Likewise, to execute an anonymous function immediately when it is defined, you place parenthesis after the function reference:



function (){
}();


That function would execute immediately. However, common convention is to first wrap the entire function definition inside of parenthesis as well. That way when reading the code, you know right at the beginning of the function that it's not just a function definition, but that it will also be executing immediately. So the example becomes:

(function (){
})();


In your original example, the function is defined to take two parameters.


(function (param1, param2){
})();


So when executing the function, any value that you want passed in would go in the parenthesis at the end. In your example, you've only included 1 value (which will be passed as param1 inside of the function).



var name = "Fotiman";
(function (param1, param2){
// param1 == "Fotiman", param2 == undefined
})(name);


"self instantiating" just means that the function is being called immediately, vs. "just another function definition" which is called (via it's identifier) at some point later.

Hope that helps.