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

Function definition of javascript study notes_javascript skills

WBOY
Release: 2016-05-16 15:52:50
Original
969 people have browsed it

Function declaration

function funname( 参数 ){

  ...执行的代码

}

Copy after login

The declarative function will not be executed immediately. It will only be executed after we call it: funname();

* Semicolon is used to separate executable JavaScript statements. Since the function declaration is not an executable statement, it does not end with a semicolon.

Function expression

var x = function( 参数 ){

  ...执行的代码块

};

Copy after login

The function defined by the function expression is actually an anonymous function (this function has no name and is stored directly in a variable)

* A semicolon is required at the end of the function expression because it is an execution statement.

Function constructor

Copy code The code is as follows:

var myFunction = new Function( "a" , "b" , "return a * b" );

Call a function and assign it to a variable:

Copy code The code is as follows:

var x = myFunction( 4 , 3 ); // x = 12;

In actual production, it is not recommended to use constructors to define functions. We can rewrite the above example as:

Copy code The code is as follows:

var myFunction = function( a,b ){ return a * b };
var x = myFunction( 4 , 3 ); // x = 12;

The above is the entire content of this article, I hope you all like it.

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