JavaScript Advanced Programming Notes (7th function expressions)

Source: Internet
Author: User

7.5 Summary

In JavaScript programming, function expressions are a very useful technique. You can use a function expression without naming the function.
So that dynamic programming is achieved. Anonymous functions, also known as lambda functions, are a powerful way to use JavaScript functions. The following summary
The characteristics of the function expression.
? function expressions are different from function declarations. A function declaration requires a name, but the function expression does not need it. Table of functions without names
The delta is also called an anonymous function.
? The recursive function becomes more complex when it is not possible to determine how to refer to a function;
? Recursive functions should always use Arguments.callee to call themselves recursively, instead of using function names--functions names can be
Can change.
When other functions are defined inside a function, a closure is created. The closure has access to all variables inside the function, and the principle
As follows.
? In a background execution environment, the scope chain of a closure contains its own scope, the scope of the containing function, and the global scope.
? Typically, the scope of a function and all of its variables are destroyed after the function is executed.
? However, when a function returns a closure, the scope of the function is persisted in memory until the closure does not exist.
Using closures can mimic block-level scopes in JavaScript (JavaScript itself does not have the concept of block-level scopes), as in the following.
? Create and call a function immediately so that you can execute the code in it without leaving a reference to the function in memory.
? As a result, all the variables inside the function are immediately destroyed-unless some variables are assigned to the containing scope (that is, the outer
The variable in the part scope).
Closures can also be used to create private variables in objects, and the concepts and key points are as follows.
? Even though there is no formal concept of private object properties in JavaScript, closures can be used to implement public methods, and the public
There are methods to access variables defined in the containing scope.
? Public methods that have access to private variables are called privileged methods.
? You can use the constructor pattern, prototype mode to implement a custom type of privileged methods, or you can use module mode, enhanced
Module mode to implement a single instance of the privileged method.
function expressions and closures in JavaScript are extremely useful features that can be used to implement many functions. However, because
Creating closures must maintain additional scopes, so excessive use of them can consume a lot of memory.

JavaScript Advanced Programming Notes (7th function expressions)

Related Article

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.