js-the Seventh chapter

Source: Internet
Author: User

1. Here's a recursive:

function factorial (num) {

if (num<= 1) {

return 1;

}else{

Return num*factorial (num-1);

}

This is a classic recursive factorial function, although the function appears to have no problem on the surface, but the following code may cause it to go wrong

var anotherfatorial =factorial;//reference to function

Factorial =null;

Alert (Anotherfatorial (4));//Error!!! Call it will be in the else inside use factorial if changed to Arguments.callee can solve this problem.

2. The principle of deep understanding of closures (involving the scope chain of the fourth chapter, 176-180 pages details why the internal anonymous function can access the contents of the function scope that called it):

Brief description: When a function returns an anonymous function B, after B is returned by a, its scope chain is initialized to the active object containing the A function (when each function is called, its active object will take both this and arguments) and the global variable object (can see the figure of 179 pages). In this way, the anonymous function can access all the variables defined in a. More importantly, when the a function finishes executing, its active object is not destroyed, because the scope chain of the anonymous function B is still referencing the active object. In other words, when a function returns, the scope chain of its execution environment is destroyed, but its active objects remain in memory, until the anonymous function is destroyed, the activity object of the A function is destroyed.

(* * Because the closure carries the scope of the function that contains it, it consumes more memory than the other functions.) Excessive use of closures may result in excessive memory consumption, and it is recommended that closures be considered only when absolutely necessary

Page 181: Examples to understand closures

3. Explain the following function, which is a function expression, which is an anonymous function that can be defined in multiplayer development to mimic a block scope (often called a private scope) whose scope is used outside the global scope to limit the addition of too many variables and functions to the global scope. Multi-person development in order to avoid too many global variables and function naming conflicts. By creating a private scope, each developer can have its own variables and not be confused about the global scope.

(function () {

This is a block level scope.

})//This parenthesis allows anonymous functions to execute immediately

Example 1:

(function () {

var now =new Date ();

if (now.getmonth () = = 0&&now.getdate () ==1) {

Alert ("Happy new year!");

}

})//This piece of code is put into the global scope, where the variable now is a local variable in the anonymous function, and we don't have to create it in the global scope

Example 2:

function one (count) {

(Fucntion () {

for (Var i=0;i<count;i++) {

alert (i);

}

})();

alert (i);//causes an error because any variable defined in the anonymous function is destroyed at the end of execution and can be accessed in the private scope because the anonymous function is a closure that can access//all variables in the containing scope

}

There is no concept of private members in 4.js, all object properties are public, but there is the concept of private variables. It is therefore possible to use closures to define privileged methods to hide data that should not be directly modified. Page 187

5. The disadvantage is that the constructor pattern must be used to achieve this, and the disadvantage is that the same set of new methods are created for each instance, and the use of static private variables to implement the privileged method avoids this problem.

(Detailed explanation of static private variables 188 page start)

6. Module Mode: The module pattern that Douglas refers to is the creation of private variables and privileged methods for a singleton. The so-called Singleton (Singleton) refers to only one instance of the object, according to the Convention, JS is the object literal way to create a singleton object. (189 pages)

js-the Seventh chapter

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.