Next we will introduce a very common ECMAScript object-function (function
The following describes a very common ECMAScript object-function ), we will explain in detail how various types of functions affect context variable objects and what each function's scope chain contains, and answer questions such as the following: what are the differences between the functions declared below? (If so, what is the difference ).
var foo = function () { ...};
Usual method:
function foo() { ...}
Or, why should the following functions be enclosed in parentheses?
(function () { ...})();
But we still need to look at them one by one, starting with the function type:
There are three function types in ECMAScript: function declaration, function expression, and function created by the function constructor. Each type 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, variable objects 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 stage (because FD already exists in VO at the context stage-before code execution ).
For example, a function is called before it is declared)
foo(); function foo() {alert('foo');}
Another key point is the second point in the above definition-the position of the function declaration in the source code:
// The function can be declared in the following places: // 1) function globalFD () {// 2) in the global context or function innerFD () in the function body of a function () {}}
Only the two positions can declare the function, that is, it cannot be defined in the expression position or a code block.
Additional reading
The topic list of this article is as follows:
- How should we understand the working principle of the JavaScript engine?
- JavaScript exploration: the importance of writing maintainable code
- JavaScript exploration: exercise caution when using global variables
- JavaScript exploration: var pre-parsing and side effects
- JavaScript exploration: for Loop (for Loops)
- JavaScript exploration: for-in loop (for-in Loops)
- Exploring JavaScript: Prototypes is too powerful
- JavaScript: eval () is the devil"
- JavaScript exploration: Using parseInt () for Numerical Conversion
- Exploring JavaScript: Basic coding specifications
- JavaScript exploration: function declaration and function expression
- JavaScript exploration: Name function expressions
- JavaScript: function name in the debugger
- JavaScript: JScript Bug
- JavaScript exploration: Memory Management of JScript
- Exploring JavaScript: SpiderMonkey's quirks
- JavaScript exploration: an alternative solution to naming function expressions
- JavaScript exploration: Object
- JavaScript exploration: Prototype chain
- JavaScript exploration: Constructor
- JavaScript probing: executable context Stack
- Execution context 1: Variable object and activity object
- Execution context 2: Scope chain Scope Chains
- Execution context 3: Closure Closures
- Execution context 4: This pointer
- Exploring JavaScript: Powerful prototype and prototype chain
- JavaScript Functions 1: function declaration
- JavaScript function 2: function expressions
- JavaScript function 3: function expressions in a group
- JavaScript function 4: function Constructor
- JavaScript variable object 1: VO Declaration
- JavaScript variable object 2: VO in different execution contexts
- JavaScript variable object 3: two stages of execution Context
- JavaScript variable object IV: Variables
- Property of the JavaScript variable object __parent _
- JavaScript scope chain 1: Scope chain Definition
- JavaScript scope chain 2: function Lifecycle
- JavaScript scope chain 3: Scope chain features
- JavaScript closure 1: Introduction to closures
- JavaScript closure 2: Implementation of closure
- JavaScript closure 3: Closure usage
Address of this article: http://www.nowamagic.net/librarys/veda/detail/1662,welcome.