By js prototype chain analytic functions that are difficult to understand the scope of the problem

Fundamental

js function is executed, the system will create an implicit attribute scope, scope scope chain is stored in the function.

Through the analysis of this scope, and JavaScript will be able to explain the problem in a number of difficult to understand:

Example 1: 
function demo () {} demo ();

scope attribute is created when the function is executed, if this function is a global function, his scope are saved in a Global object and a activation object.

global object is stored in the global information and activition object information is stored inside a function, such as which have variable functions, etc.

例2:
     function demo(){ function inner(){} } demo();

If this function is an internal function, he inherits the prototype chain of the parent letter, and create your own activation at its tip.

When the function is finished, the function prototype chain is destroyed.

Why not call the subroutine function outside the function

As shown in Example 2:

When the demo function is finished, the function prototype chain is destroyed, there is no way of knowing the information inside the function on the outside. You can not call it inner function

Why subroutine can use a variable function of the parent

Functions inherits the prototype chain because the parent function, he can pass the parent function in the prototype chain activation object found on the parent function defined variables

 

Guess you like

Origin www.cnblogs.com/iszhangk/p/10994946.html