js Learning Difficulties knowledge summary (consolidated closures, prototype, prototype chain)

Learning knowledge summary
 
1. Closure knowledge consolidation
       Closure function:
                   1. The function may be implemented external variable access intrinsics
                    2. JavaScript can be read only in the local variables inside a function subroutine, the closure can be simply interpreted as "a defined function within the function."
               use:
                    1. You can read the local variables inside a function
                    2. Let these variables are always stored in the memory of them (extend the life cycle of a local variable)
   Closed package scene appears:
        1: external functions can access variables inside a function
        2: In the immediate execution of the function
        3: closure may remain inside the memory variables (binding event to the set of nodes, each node acquires the index value)
         4: Function currying
 
// interview questions: the results of a function call fn (2) (3) (4) to get bit 24; this function is what is written ??
    function fn(a){
        return function(b){
            return function(c){
                console.log(a * b * c);
            }
        }
    }
    fn(2)(3)(4);
    // function currying -> use closures.
 
    Closures Impact:
        1, due to the closures will make the function of the variables are stored in the memory of them, will consume a lot of memory, it is not possible misuse closure, otherwise it will cause problems webpage performance in IE that may cause a memory leak   
        2, the value of the variable Closures external function, a function of changing the internal. Otherwise you will become a private variable common variables, this is not acceptable. So it should not change the value of the parent function of the internal variables
       
                Do not deliberately use closures, minimize the use of closures.
                Closures will open up the memory, it can cause a memory leak
    Expansion: js garbage recycling machine
        1: Functions to open up in the implementation of variable space in memory, stored functions, if the function completes the memory began to open up in time to release the calling function, variable initialization continue repeat the above steps...
        2: If the internal variable is a function of the relevant circumstances cited the variable will not be withdrawn.
 
Interview Related:
        What is closure:
            1: external functions can access variables inside a function
            2: Closures function and prolong the lifetime of the internal variable, the variable value is not destroyed in a memory
 
Examples to explain:
     Objective: To re-function external access variables inside the function.
     function fn(){
        There are a = 10;
         function return () {          // return a function fn which (closure function)
            a++
             return a;
         }
     }
     was n = fn ();
     console.log(n());
 
     function fn(){
         There are a = 10;
         a++
      console.log(a);
      }
     fn();
     fn();
     fn();
     fn();
     fn();
2. Object-Oriented Review
    Thoughts: When multiple instances of an object inside the method used is consistent.
                     function person(name,age){
                        this.name = name;
                        this.age = age;
                        this.eat = function(){
                        }
                        this.sleep = function(){}
                    }
                    var p1 = new person('ws','10');
                    var p2 = new person('x','30');
                    console.log (p1.eat == p2.eat); // false as long as the new went back in memory to open up a space.
            prototype:
                    Public storage properties and methods
                    Extended attributes and methods
                    Save memory
                    Inheritance !!!!!!!!!!
                 例如:function person(name,age){
                        this.name = name;
                        this.age = age;
                    }
                    // person.prototype.eat = function(){}
                    // person.prototype.sleep = function(){}
                    person.prototype = {
                        eat:function(){},
                        sleep : function(){}
                    }
3. Scope problem
        Local scope (scope function)
        Global scope
        Scope chain: chain variable to find the way to the first visit itself, if you can not find the current variable itself, looks to the parent function.
4. prototype chain issues
 
//p1.work (); // instantiate an object on the direct access method constructor prototype.
    //console.dir(Person); // constructor function prototype there is a method in the above
    // print where p1 observed in the instance of the object inside the method? ? ? ? ? ? ?
    //console.dir(p1); // instantiate an object located in a method of the above __proto__
    /*
        analysis:
            Because p1 is the constructor of Person constructed.
            Person on the prototype method
            Examples of the method in object p1 __proto__
            Conjecture: Person.prototype and p1 .__ proto__ is pointing to the same object.
            Person.prototype == p1.__proto__        //true
            Person.prototype and p1 .__ proto__ to a prototype object is the same
            p1 .__ proto __. eat () p1 prototype object by object instance constructor __proto__ access points Person.prototype
            p1.__proto__.__proto__.eat();
 
Prototype chain: link instance object prototype object called prototype chain. Representation __proto__  
                Prototype chain lookup process:
                    In the first instance of the object to find its own property, if there is a direct return,
                    If there is no current property, find the prototype object constructor prototype of access by __proto__ structure of the object.    
                    If the current constructor is still not, by __proto__ began to Object.protoptype Find /
 
prototype and constructor __proto__ and the relationship between (constructor)? ? ?
            Only the presence of the above function prototype points to a prototype object
               
            It refers to an object which exists __proto__ prototype object constructor prototype points.
            constructor is configured pointing configuration example of object constructor
 

Guess you like

Origin www.cnblogs.com/wangwenxin123/p/11266981.html