The JS function can be defined in the following three ways
1. Define functions using the keyword function.
2, can be defined by the declaration.
3, it can also be an expression.
1. Keyword function definition (constructor)
var New Function (' A ', ' B ', ' return a * b '); var x = MyFunction (4, 3);
It is not necessary to actually use constructors (many times in JavaScript you need to avoid using the new keyword, and this approach can lead to parsing of two of code, affecting performance.) Parse the regular JavaScript code for the first time and parse the string passed in the constructor for the second time. The above example can be written as follows:
var function (A, B) {return A * B} var x = MyFunction (4, 3);
2, the declaration definition, in the manner of the expression than the parser will first read the function declaration and make it available (accessible) before executing any code, this is due to the JavaScript function declaration elevation;
function functionname () { // execute code }
3, the expression method definition, namely the anonymous function, the expression definition must wait until the parser executes to its line of code, only then will actually be interpreted executes
var function (A, B) {return A * b};
Self-invoking function (actually a function that is called anonymously)
function expressions can "self-invoke". Self-invocation expressions are called automatically and cannot be called from a declared function.
(function() { // Code }) ();;
The JS function is an object:
1, typeof function ();//return function.
2, the function has the property and the method.
3. The Arguments.length property returns the number of arguments received by the function call procedure.
function MyFunction (A, b) { return arguments.length;}
4, the ToString () method returns the function as a string.
function MyFunction (A, b) { return A * b;} var txt = myfunction.tostring (); // function MyFunction (A, b) {return a * b}
The definition of JS function