Home > Web Front-end > JS Tutorial > Named functions and unnamed functions in javascript_javascript skills

Named functions and unnamed functions in javascript_javascript skills

WBOY
Release: 2016-05-16 19:08:18
Original
1653 people have browsed it

Look at an example: 1

Copy code The code is as follows:

function a() {
alert("I am a script home");
}


2
Copy code The code is as follows:

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");
}
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template