Functions and closures in JS

Source: Internet
Author: User

JavaScript functions greatly enhance the programming power of JS because of its unique functions and features. In JS, the function is also an object, so you can add properties and methods to it, but the typeof operator returns the string ' Fnuction ' to the function, and the native ToString () method returns the source code that represents the function.

First, the essence of the function
The function itself is also a variable created in JS (exactly classified as an object), which consists of a series of executable code +name attributes + formal parameter lists. Functions can be called by the Name property or by a variable name. A function has a function scope that controls the transformation of the program execution environment relative to the global scope.

Second, the definition of function
Functions can be defined in three ways, the most common is the var variable declaration, which is the function definition expression and the Functions keyword declaration. The difference between the two methods is that: 1, the function declaration will make the function declaration promotion, that is, the function can be legally used before the declaration of functions, function definition expression will only make the function name in advance, but the executable code is not declared; 2, each function has a Name property, which represents the name of the function, functions defined with function declarations the Name property is not empty, and a function defined with a function definition expression becomes an anonymous function, and the Name property is an empty string. It is not recommended to use the constructor pattern to define functions in JS, which consumes memory and is less efficient.

Third, local scope of function
There is no block-level scope in JavaScript, there is no local variable inside the for loop structure. But the function has the local scope, the function internal parameter list, the variable, the inside function all belong to this local scope the local variable or the function, can only be used inside the function, generally when the function execution finishes (namely encounters the return statement or executes at the end of the code) will be treated as "garbage" by the JS memory management mechanism Recycled.

Iv. Invocation of functions
1, as the object's method call, at this time the execution context is the object;
2, as a normal function call, whether it is called in the global or inside a function, the execution context is global scope;
3, as a constructor call, at this time the execution context is the newly created object instance;
4. The execution context is the object that indirectly calls the function when the call or Apply method is used as a function invocation in the impersonation of an object.

Five, function lexical analysis process
Before the function runs, there is a pre-compilation, which is the lexical parsing process, as follows:
1, the analysis parameter list, the shape parametric as the attribute in the AO activity object, the attribute value is undifined;
2, the analysis function inside declares the variable, if does not have this attribute in the Ao object, then holds this property, the value is undifined, if already has this attribute to do nothing;
3, parse the inline function, if the AO does not have the same name attribute, then add this property (as a method to save), if there is a property with the same name, then overwrite the previous property.
The function lexical analysis only determines which properties are in the AO object of the function, whether it is a variable or a function, the value of the variable is undifined, and the property in the AO object is assigned a value when the function is run!

Six, the best feature of the function--closure

1, the formation principle and the essence of closure
A closure is a function that is embedded in a function, so its scope chain associates all the outer functions and the variable objects of the global scope, meaning that it can access all the outer functions and variable objects in the global scope. The variable object of the global scope is not recycled until the end of the program execution, as described above, where the variable object of the outer function is normally recycled after the function execution is finished. However, if this occurs, this or these closures are returned by the outer function as the return value or as a return value (the outer function returns an object, the closure is returned as a method of the object), and several closures share these lingering variable objects. In short, the closure is preserved, then the variable object of the closure itself will not be destroyed, and more importantly, the closure of the largest role-it will be associated with the variable object (that is, all the variables of the outer function of the variable object) are stuck in memory, will not be recycled! From the perspective of using variables, those variable objects can only be accessed through this closure.

2, closure of the role
(1) Realize the caching mechanism, save the computer resources, speed up the execution speed and improve the efficiency;
(2) to realize the encapsulation of private variables;
(3) Implement object-oriented objects.

3. Problems caused by closures
(1), may cause the circular reference causes the memory leak;
(2), due to the closure of memory, will be more consumption of resources;
(3), the same execution results generated by the closure of the shared variable object, may cause unexpected results of developers, such as in the loop to bind the event handler function.

Original address: www.bfy0412.com

Functions and closures in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.