JavaScript Advanced Programming (3rd Edition) Chapter Seventh reading notes

Source: Internet
Author: User

Seventh Chapter function Expression

1. Function declarations have an important feature, and function declarations are promoted. That is, the function declaration is read before executing the code, which means that the function declaration can be placed after the statement that called it.

2. Use Arguments.callee to implement recursive calls to functions. However, in strict mode, Arguments.callee cannot be accessed through a script, but you can use a named function expression to achieve the same effect.

3. Closures refer to functions that have access to variables in another function scope. A common way to create closures is to create another function inside one function.

4. Because the closure carries the scope of the function that contains it, it takes up more memory than the other functions. Use cautiously.

5. Closures can only get the last value that contains any variable in the function. A closure holds the entire variable object.

6. The use of this object in closures can also cause problems where the execution environment of an anonymous function is global, so its this object usually points to window.

var name = "the window"; var object = {         Name: "My object",         getnamefunc:function () {                   return this.name;         }}; Console.log (Object.getnamefunc ());    My object var name = "the window"; var object = {         Name: "My Object",         getnamefunc:function () {                   return functio N () {                            return this.name;                   };}         }; Console.log (Object.getnamefunc ());    The window

  

By saving the This object in an external scope to a variable that the closure can access, you can have the closure access the object.

1 varName = "the window";2 3 varObject = {4 5Name: "My Object",6 7Getnamefunc:function(){8 9                    varthat= This;Ten  One                    return function(){ A  -                             returnThat.name; -  the                    }; -  -          } -  + }; -  +Console.log (Object.getnamefunc ());//My Object

7. Closures can cause special problems in versions prior to IE9, which means that the element will not be destroyed if an HTML element is stored in the scope chain of the closure.

8. Syntax for anonymous functions that mimic block-level scopes:

(function () {       //This is block-level scope});

  

The code above defines and immediately invokes an anonymous function that contains a function declaration in a pair of parentheses, indicating that it is actually a function expression, and that the other pair of parentheses immediately following it will call the dry function. For the party, it can be written like this:

var someFunction = function () {   //This is a block-level scope};somefunction ();

  

Note: The function expression can be followed by parentheses, but the function declaration is not followed by parentheses, the function declaration Jiashan followed by a pair of parentheses can be converted into a function expression.

9. Impersonation of block-level scopes is often used outside the function in the global scope to limit the addition of too many variables and functions to the global scope. This approach reduces the memory problems that are used by closures, because there is no direct reference to the anonymous function, and as long as the function is executed, its scope chain can be destroyed immediately.

10. Any variable defined in a function can be considered to be a private variable, because these variables cannot be accessed outside of the function.

11. Public methods that have access to private variables and private functions are called privileged methods. The first is to define the privileged method in the constructor. Privileged methods as closures have access to all variables and functions defined in the constructor.

A static private variable that creates a privileged method by defining a private variable or function in the private scope. This pattern uses function expressions instead of function declarations when defining constructors.

JavaScript Advanced Programming (3rd Edition) Chapter Seventh reading notes

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.