The best JavaScript function definition Tutorial In 2024, In this tutorial you can learn Function declaration,Examples,Function expression,Examples,Examples,Function () constructor,Examples,Examples,Function to enhance (Hoisting),Since the function is called,Examples,Function can be used as a value for use,Examples,Examples,Functions are objects,Examples,Examples,
JavaScript function is defined using the keyword function.
You can define a function declaration, it can be an expression.
In the previous tutorial, you already know the syntax of a function declaration:
After the function declaration does not happen immediately, it will call when we need to.
The semicolon is used to separate executable JavaScript statement. Since the function declaration is not an executable statement, so it does not end with a semicolon. |
JavaScript functions can be defined by an expression.
Function expression may be stored in a variable:
After the function expression is stored in a variable, but also as a function Use:
The above function is actually an anonymous function (function without a name).
Functions are stored in a variable, the function name is not required, usually called by the variable name.
Above functions with a semicolon, because it is an executable statement. |
In the above example, we learned that functions with the keyword function definitions.
The same function can (Function ()) defined by the built-in JavaScript function constructor.
In fact, you do not have to use the constructor. Examples of the above can be written as:
In JavaScript, a lot of the time, you need to avoid using the new keyword. |
In the previous tutorial, we have learned "hoisting (lifting)."
Lift (Hoisting) JavaScript is the default of the current scope to the front to enhance the behavior.
Lift (Hoisting) Application Disclaimer and function of variables.
Therefore, the function can be invoked before the statement:
Unable to enhance the use of expressions defined functions.
Function expression can be "self-call."
Since the call expression invoked automatically.
If after an expression followed () is automatically called.
Function declaration can not call itself.
By adding brackets, to illustrate that it is a function expression:
The above function is actually an anonymous function that calls itself (not the function name).
JavaScript function as a value to use:
JavaScript functions can be used as expressions:
Use typeof operator to determine the type of function in JavaScript returns "function".
But JavaScript function described as a target more accurately.
JavaScript functions have attributes and methods.
arguments.length property returns a function call received to the number of parameters:
toString () method will function as a string:
Function is defined as a property of the object, called object method. If the function is used to create a new object, called object's constructor. |