javascript study notes function definition

  • 2020-06-19 09:50:17
  • OfStack

Function declaration


function funname(  parameter  ){

  ... Executed code 

}

The declared function does not execute immediately and requires a call: funname();

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

Functional expression


var x = function(  parameter  ){

  ... Block of code to execute 

};

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

* The function expression ends with a semicolon because it is an execution statement.

The Function constructor


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

Call the function and assign a variable:


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

In practice, it is not recommended to use constructors to define functions. The above examples can be rewritten as:


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

This is the end of this article, I hope you enjoy it.


Related articles: