I. Best Practice for Object Inheritance
What a “class” is made of in JavaScript:
- [private stuff] The
constructorfunction. This function contains all the logic to create an instance of the “class”, i.e. instance specific code . - [public stuff] The
prototypeobject. This is the object the instance inherits from. It contains all methods (and other properties) that should be shared among all instances.
The
constructorcorresponds toprivate, andprototypecorresponds topublicin Java
Steps to inherit from another object:
consturctor inherit
prototype inherit
Benefits of using
Object.create(Person)for inheritance rather thannew Person()?Actully this works because an
Person instanceinherits from thePerson prototypeobject. But it also implies that every dog inherits from one specificPersoninstance. That seems to be a bit strange. Shouldn’t instance specific code only be run in the constructor function? Suddenly instance specific code and prototype methods seem to be mixed.We don’t actually want to run
Personinstance specific code at that moment, we only want all the methods from thePerson prototypeobject. That is whatObject.createlets us do
prototype constructor correct
1 | /* parent object: Person */ |
II. prototype vs __proto__
in JS, everything is an
objectexcept primitive value. objects can serve 2 goals:prototype/classfor othersub-object/sub-class. In this case, we can checkprototypeof aconstructor‘s shared parent object [ie. prototype]sub-object/sub-classof otherprototype/class- NOTE:
- an object created by
new(constructor) doesn’t hasprototypeproperty, since it’s created with the hope to get an pure object! - If this object wants to have
prototypeproperty, then it must be the case we want this object to be a ‘super-class’, and inherited by ‘sub-class’. This way, we will need to attachprototypeto this object. Also, we need to create shared field or method ontoprototype(otherwise, if nothing insideprototype, are we dumb to create all fields in memory without putting shared stuff inprototype??? ) - On the other hands, an object created by
new(constructor) always has__proto__property, in which we can know:- the
constructorof this object - other shared
methodsorfieldsof itsprototype
- the
- An
prototypewill always have aconstructorproperty, to tell its corresponding U associated to this ‘class’ prototype
- an object created by
prototypeis the object that is used to build__proto__when you create an object withnew. It is used byconstructor()functions. It should’ve really been called something like, “prototypeToInstall”, since that’s what it is.__proto__is the actual object that is used in the lookup chain to resolve methods, pointing to its prototype. It is that “installed prototype” on an object (that was created/installed upon the object from saidconstructor()function)1
2( new Foo ).__proto__ === Foo.prototype;
( new Foo ).prototype === undefined;
Reference
- https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance
- https://stackoverflow.com/questions/17392857/benefits-of-using-object-create-for-inheritance
- https://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor
- https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript