May 06, 2021 JavaScript
1. The JavaScript function defines
5.. Function Promotion (Hoisting)
8.. The function can be used as a value
JavaScript defines functions using the keyword function.
Functions can be defined by declaration or by an expression.
In the previous tutorial, you learned about the syntax of function declarations:
Function declarations are not executed immediately and are called when we need them.
A sign is used to separate executable JavaScript statements.
Because the function declaration is not an executable statement, it does not end with a sign. |
JavaScript functions can be defined by an expression.
Function expressions can be stored in variables:
After a function expression is stored in a variable, the variable can also be used as a function:
The above function is actually an anonymous function (the function has no name).
Functions are stored in variables and do not require function names, which are usually called by variable names.
The above function ends with a sign because it is an execution statement. |
In the above example, we learned that functions are defined by the keyword function.
Functions can also be defined by the built-in JavaScript function constructor (Function)).
In fact, you don't have to use constructors. The above example can be written as:
In JavaScript, there are times when you need to avoid using the new keyword. |
We've seen "hoisting" in previous tutorials.
Hoisting is JavaScript's default behavior to elevate the current scope to the front.
Hoisting is applied to the declaration of variables and the declaration of functions.
Therefore, the function can be called before declaring:
You cannot promote a function when you define it with an expression.
Function expressions can be "self-called."
The self-calling expression is called automatically.
If the expression is followed by (), it is automatically called.
Y cannot self-call declared functions.
By adding parentheses, it is a function expression:
The above function is actually an anonymous self-called function (no function name).
The JavaScript function is used as a value:
JavaScript functions can be used as expressions:
Using the typeof operator in JavaScript to determine the function type returns "function".
However, the JavaScript function is more accurate in describing an object.
JavaScript functions have properties and methods.
The arguments.length property returns the number of arguments received by the function call procedure:
The toString() method returns the function as a string:
A function defines a property as an object, called an object method.
A function is called an object constructor if it is used to create a new object. |
JavaScript function definition
In JavaScript, extract the duplicate parts of the code and put them in a function.
JavaScript defines functions with parameters
The parameters of the JavaScript function, parameters, act as placeholders (also known as parameters), and the arguments can be one or more. T he arguments passed in when a function is called are arguments, which determine the true value of the parameters.