A little summary of the js function

1 this function scopes

this scope is determined according to the current environment, you can use the method to call and apply this point to change the current

    <Script>
         var name = "Global" ;
         function Fn () { 
            the console.log ( the this .name); 
        }; 
        Fn (); // function declarative, this global point, "Global" 
        var obj = { 
            name: " Object " , 
            say: function () { 
                the console.log ( the this .name); 
            } 
        } 
        obj.say (); // call objects, this points to the current object," Object " 
        function the Person () {
             the this .name =" the Person " ;
            this= ... SAY function () { 
                the console.log ( the this .name); 
            } 
        } 
        var P = new new the Person (); 
        p.say (); // constructor calls this constructor point, "Person" 
    </ Script>

2 functions inherited methods

The constructor function prototypes and inheritance

Call call in the constructor, inheritance

    <script>
        function Parent(){
            this.name = "zzz";
            this.age = 233;
        };
        function Child(){
            Parent.call(this);
            this.favor = "dance";
        }
        var p = new Parent();
        var c = new Child();
        console.log(p);//{name: "zzz", age: 233}
        console.log(c);//{name: "zzz", age: 233, favor: "dance"}
    </script>

Prototype inheritance chain

    <script>
        function Parent(){
            this.name = "zzz";
            this.age = 23;
        };
        function Child(){
            this.favor = "dance";
        }
        Parent.prototype.id=233;
        Child.prototype = new Parent();
        var c = new Child();
        console.log(c.id);//233
    </script>

Guess you like

Origin www.cnblogs.com/Zxq-zn/p/11479547.html