1.JavaScript function Definition
JavaScript uses the keyword function to define functions.
A function can be defined by a declaration, or it can be an expression.
1.1 Function declarations
In the previous tutorial, you've learned about the syntax of function declarations:
function functionname (parameters) { executed code}
Function declarations are not executed immediately and will be called when we need them.
function MyFunction (A, b) { return A * b;}
1.2 Function expressions
JavaScript functions can be defined by an expression.
function expressions can be stored in variables:
var x = function (A, b) {return A * b};
After a function expression is stored in a variable, the variable can also be used as a function:
var x = function (A, b) {return A * B}; var z = x (43);
The above function is actually an anonymous function (the function has no name).
Functions are stored in variables and do not require a function name, usually called by a variable name.
1.3Function () constructor
In the above example, we understand that the function is defined by the keyword function.
Functions can also be defined by the built-in JavaScript function constructor (functions ()).
var New Function ("a""b""return a * b " ); var x = myFunction (43);
In fact, you don't have to use constructors. The above examples can be written as:
var myFunction = function (A, b) {return A * b}var x = myFunction (4 3);
1.4 function boost (hoisting)
In the previous tutorial we have already learned about "hoisting (Ascension)".
Elevation (hoisting) is the behavior by which JavaScript promotes the current scope to the front by default.
Elevation (hoisting) applies the declaration of a variable to a statement of the function.
Therefore, the function can be called before the declaration:
MyFunction (5); function myFunction (y) { return y * y;}
cannot be promoted when defining a function with an expression.
1.5 self-calling functions
function expressions can "self-invoke".
The self-invocation expression is called automatically.
If the expression is immediately followed by (), it is called automatically.
A declared function cannot be called from itself.
By adding parentheses, you can say that it is a function expression:
(function () { var"hello!! "; // I will call myself }) ();
The above function is actually a function of anonymous self-invocation (without the name of the letter).
The 1.6 function can be used as a value
JavaScript functions are used as a value:
function MyFunction (A, b) { return A * b;} var x = myFunction (43);
JavaScript functions can be used as expressions:
function MyFunction (A, b) { return A * b;} var x = myFunction (432;
1.7 Functions are objects
Using the typeof operator in JavaScript to determine the function type will return "functions".
But JavaScript functions are described as an object that is more accurate.
JavaScript functions have properties and methods .
The Arguments.length property returns the number of arguments received by the function call procedure:
function MyFunction (A, b) { return arguments.length;}
The ToString () method returns a function as a string:
function MyFunction (A, b) { return A * b;} var txt = myfunction.tostring ();
JavaScript function definitions