Home > Web Front-end > JS Tutorial > body text

Detailed explanation of function declaration, promotion, duplication and deletion examples of the basics of javascript functions

伊谢尔伦
Release: 2017-07-25 09:50:45
Original
1625 people have browsed it

Function declaration statement

Use the function keyword, followed by a set of parameters and the function body

function funcname([arg1 [,arg2 [...,argn]]]){
    statement;
}
Copy after login

funcname is the identifier of the function name to be declared. The parameter list is enclosed in parentheses after the function name, separated by commas. When calling a function, these identifiers refer to the actual parameters passed into the function

[Note] The curly braces in the function statement are required, which is the same as the statement block used in while loops and other statements. Differently, even if the function body contains only one statement, it must still be enclosed in curly braces

function test()//SyntaxError: Unexpected end of input
function test(){};//不报错
while(true);//不报错
Copy after login

Promotion

In the third article of the scope blog series, function declarations were mentioned Hoisting, the function name and function body are hoisted

foo();
function foo(){
    console.log(1);//1
}
Copy after login

The reason why the above code snippet can output 1 on the console is because the foo() function declaration is hoisted, as shown below:

function foo(){
    console.log(1);
}
foo();
Copy after login

Duplication

The repeated declaration of a variable is useless, but the repeated declaration of a function will overwrite the previous declaration (whether it is a variable or a function declaration)

//变量的重复声明无用var a = 1;
var a;
console.log(a);//1
Copy after login
//由于函数声明提升优先于变量声明提升,所以变量的声明无作用
var a;
function a(){
    console.log(1);
}
a();//1
Copy after login
//后面的函数声明会覆盖前面的函数声明
a();//2
function a(){
    console.log(1);
}
function a(){
    console.log(2);
}
Copy after login

Therefore, you should avoid using the same Duplicate declarations in the scope

Delete

Like variable declarations, variables created by function declaration statements cannot be deleted

function foo(){
    console.log(1);
}
delete foo;//false
console.log(foo());//1
Copy after login

The above is the detailed content of Detailed explanation of function declaration, promotion, duplication and deletion examples of the basics of javascript functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!