Skip to content Skip to sidebar Skip to footer

Correct Prototype Chain In Console (with Object Inheritance)

Forgive me for being bothered by something that works, yet not showing up 'as expected' on the console. Consider the following code: function Person() {}; Person.prototype.PersonAc

Solution 1:

This will do it by setting the constructor back to what it's supposed to be and using Object.create() for the prototype:

functionMammal() {};
Mammal.prototype.MammalAction = function() {}

functionPerson() {};
Person.prototype = Object.create(Mammal.prototype);
Person.prototype.PersonAction = function() {}
Person.prototype.constructor = Person;

console.log( newPerson() );

It gives you this in Chrome:

Person {PersonAction: function, constructor: function, MammalAction: function}
    __proto__: PersonPersonAction: function () {}
        constructor: functionPerson() {}
       __proto__: MammalMammalAction: function () {}
           constructor: functionMammal() {}
           __proto__: Object

Demo: http://jsfiddle.net/jfriend00/CJF3L/

Solution 2:

It is correct, you set Person prototype with an instance of mammal and added a property personaction.

I assume personaction is not a constructor so it should not be capitalizes, its better to use object.create instead of creating an instance of parent to set the prototype part of inheritance.

More info here: Prototypical inheritance - writing up

Post a Comment for "Correct Prototype Chain In Console (with Object Inheritance)"