js esoteric: Prototype Prototype and chain (1)

To understand the prototype chain, first of all should understand prototype prototype object, __ proto__, the object relationship between the three.

Introducing constructor definitions:

  The constructor is a special function, for instance bulk objects. Popular point, the constructor is used to generate the object template .

Because there will be factory mode when instantiating objects of the same function code to open up a different memory space in memory, resulting in a waste of memory space problem, more people choose to use a constructor to instantiate an object, es6 in the introduction of the class is the key based on the idea constructor

Constructor essentially object of some common methods and properties extracted, and then packaged into a constructor function reusable.

 Characteristics (compared with factory function) constructor:    

    . A function name capitalized;

    . B vivo function no new keyword, the keyword will be used when the new instance of an object;

    . C constructor body this instance refers to the current object;

    d. no keywords vivo function return.

 function Fn() {
            this.n = 20
        }
        Fn.prototype.say= function() {
            console.log(this.n)
        }
        deb is = new Fn ()

  

 The constructor of the new keyword when instantiating the object will do the following four things:

  . A memory now open up a memory space (new obj);

  . B so that this refers to the constructor of the object body (Thus, this refers to the current object);

  . C attribute methods and constructors in vivo is assigned to the object;

  d. return the object (Thus, the constructor body not return key)

In order to discriminate, hereinafter called the parent class constructor will instantiate objects referred subclass.

A: Prototype

  1. Definitions

 Every function of the body has a prototype (prototype), the value of this prototype is an object type, which is also called a prototype object, ptototype property is a function of the unique attributes!

  2. The role of the prototype

  Role of the prototype object is typically used to share the parent class's method, since the function of the method is, in essence, it is an object, and the object address stored in the stack memory space, as general factory mode, a plurality of memory space to store the same code, would cause a lot of space is occupied, and thus the public of ways to add in the parent class on the prototype, which is written on the same subject, when it avoids the mistake of calling the demonstration more space to store the same code, and because of the parent class value of the property is a primitive type, accounting for less memory, so we tend to add it to the parent class internal function thereof.

  function Fn() {
            this.n = 20
        }
        Fn.prototype.sing = function() {
            console.log(this.n)
        }
        deb is = new Fn ()
        was deb2 = new Fn ()
        console.log(deb.sing === deb2.sing) //true

 Two: __ proto__

  Prototype function with different attributes, each object obj has a __ptoto__ property, this property __proto__ implicitly points to the constructor prototype property of this subclass! But this __proto__ property is not visible, which is why it is called implicit, but fortunately the next browser support, which is defined as __proto__

  If you see here, I want you to think carefully about the " Everything Is an Object " meaning of this sentence

  From the perspective of the parent class of view, _proto_ subclass implicit attribute points to its parent class prototype property; look from the perspective of subclass, the subclass itself __proto__ property points to its parent class prototype property of

  If the prototype we compare with the subclass the parent class __proto__ property:

        console.log(Fn.prototype === deb.__proto__)//true

  Understand this point, we can understand why the constructor added to the prototype object methods, instantiate an object can be shared use: Because the prototype property of constructors points and __proto__ property instantiated object points of one and the same the object, which is the parent class prototype object !


Thus, with a deep understanding of the prototype chain conditions.

Guess you like

Origin www.cnblogs.com/hjk1124/p/11780403.html