Prototype chain summary

Because the prototype object person defines common method of say, though it appears after we instantiate an object, but this prototype is declared before the method call, so beneath him instantiated object can have its say this method.
Through this case we can understand, use the prototype object is to store some common methods and properties for each instance of the object. . == new new person.say the Person () say
person.say IS Not A function
when var person = new Person (), Person.prototype is: Person {} (Of course, there are internal constructor property), i.e., Person. prototype points to an empty object {}. For example person, its internal pointer to a prototype chain proto, the pointer points to an object Person.prototype points, i.e., {}. Then reset the Person prototype object to point to another object, the Object {say: function}, the time point is not changed person.proto, {} the object it points to say there is no method, because the error.

function Person () {
        this.name = 'John';
    }
    Person.prototype = {
        say: function() {
            console.log('Hello,' + this.name);
        }
    };
    var person = new Person();
    person.say();//person.say is not a function

Prototype object constructor function defaults to the function itself, in addition to the prototype object prototype attributes, inheritance order, there is a prototype of the __proto__ chain pointer, the pointer points to an object on the layer of the prototype, the prototype of the object on the layer Similarly still, this point has been on the use of __proto__ Object prototype object, and Object prototype object. with Object.prototype proto = null represents the top of the prototype chain, so morphed into javascript prototype inheritance chain, also explains Why all the objects have javascript basic method of Object.

the object is a preset function prototype above object properties
1.JS all things are objects, each prototype has this property, this property is a target (object)
in the Object 2.JS everything by derivatization to, i.e., the end point of the prototype chain everything Object.prototype
subtle relationship 3.JS the constructor and instances (objects) between convention constructor specifications defined by the prototype examples, an example of re-constructed by the new their role is to produce objects. Constructed functions (methods) which in turn is an instance method (Function), and therefore can be found in its proto (prototype chain)
Object / function F. () {} Like this is the constructor, one JS native API provided , is a custom
new Object () / new F ( ) is an instance like this
instance is "only" proto to see that they are based on what Pro
toType be produced, and "can not" re-defined instances prototype delusion create instances of the instance.
Prototype
is a prototype of the above objects, this prototype is usually the prototype property its constructor
prototype chain

function foo(){};
foo.prototype.z = 3;
var obj = new foo();

By new configuration objects (instances) it is characterized, obj prototype (prototype) pointing constructor prototype property, i.e. Foo.prototype, and points to the original Foo.prototype Object.prototype, Object.prototype also prototypes, It is null. This is a whole prototype chain.
When a prototype
advantage of using the prototype is not going to produce additional memory, after all instantiated objects will inherit this method from the prototype. That is, the need for a sub-category has certain characteristics of the parent class (the same kind of characteristics can be covered), they can add their own features, without affecting the parent class when using the prototype.

Guess you like

Origin blog.csdn.net/qq_27674439/article/details/91994971
Recommended