Look at an example: 1
function a() {
alert("I am a script home");
}
2
var a = function(){
alert("I am a script home");
}
Methods 1 and 2 are equivalent. 1 is a named function, while 2 just lets a variable point to an unnamed function. 1 and 2 are equivalent here. 2 You can add parentheses directly after the function declaration to indicate that the function call will be made immediately after the creation is completed. For example:
var i = function(obj){
alert(obj);
}("I am a script home");
Another important difference between named functions and unnamed functions: for named functions A function can be defined after it is called; for an unnamed function, it must be defined before it is called. For example, the following error example of using an unnamed function:
i();
var i = function(){
alert("I am a script home");
}
The following is Correct way to write:
var i = function(){
alert("I am a script home");
}
i();
Or use a famous function:
i ();
function i(){
alert("I am a script home");
}