Home > Web Front-end > JS Tutorial > Introduction to three ways of naming javascript functions and their differences_Basic knowledge

Introduction to three ways of naming javascript functions and their differences_Basic knowledge

WBOY
Release: 2016-05-16 15:08:29
Original
1362 people have browsed it

Introduction to the three ways of naming JavaScript functions and their differences

First place

Copy code The code is as follows:

function fn(val1,val2) {
alert(val1+val2);
}
fn(1,2);

Second

Copy code The code is as follows:

var fn=function() {
alert(val1+val2);
}
fn(1,2);

Third

Copy code The code is as follows:

var fn=new Function("alert(val1+val2)");
fn(1,2);

The above three methods are logically equivalent, but there are still some small differences: Difference 1: The function in Example 1 will be loaded into the scope before the code is executed, while in Example 2 it will be loaded into the scope after the code is executed. There will be a definition only when it is one line; Difference 2: The function declaration will assign a name to the function, while the function expression creates an anonymous function and then assigns the anonymous function to a variable; Difference 3: Example 3 uses Function() Constructor clone functions are generally rarely used because a function usually consists of multiple statements. If they are passed as parameters in the form of strings, it will inevitably make the code less readable.

The above three ways of naming JavaScript functions and their differences are all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home more.

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