Introduction
In this article, we will discuss in more detail a common ECMAScript object-function. We will study in detail the various types of functions and explain how these types of functions affect a context variable object and what is included in the scope chain of a function of a certain type. We will answer questions frequently asked in the Forum: "What is the difference between the function created below and the function defined in a" habitual way "(if any, what is it ?)" (Note: I have adjusted the order of the original text)
var foo = function () { ...};
Functions defined in a "habitual way:
function foo() { ...}
Or, "Why should the following function calls be surrounded by parentheses ?"
(function () { ...})();
Because these articles are independent from earlier chapters, and because we will actively use the syntax from these chapters, to fully understand this part, it is worth reading chapter 2 if necessary: variable object and Chapter 4: Scope chain.
But let's proceed one by one, starting with studying the function type.
Function Type
There are three function types in ECMAScript, each of which has its own characteristics.
Function Declaration
Function declaration (FD) is a function:
- There is a specific name
- Position in the source code: either in the Program level or in the FunctionBody of other functions
- Create in the context stage
- Variable objects
- The following statement
function exampleFunc() { ...}
The main feature of this function type is that they only affect variable objects (that is, the function is stored in the context VO ). This feature also explains the second important point (it is the result of the properties of the variable object)-they are available in the code execution phase (because FD has been collected by VO In the context phase ).
For example, (functions in the source code are called before they are declared)
foo();function foo() { alert('foo');}
Another important point is the second point in the definition-the position of the function declaration in the source code:
// function declaration// is directly in:// either the global context// at Program levelfunction globalFD() { // or directly inside the body // of another function function innerFD() {}}
Function declaration cannot appear anywhere in the code -- that is, it cannot be defined in the expression position or a code block.
Instead of the function declaration (more likely to be considered opposite to it), it is a function expression.
- 7 pages in total:
- Previous Page
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- Next Page