I. Best Practice for Object Inheritance
What a “class” is made of in JavaScript:
- [private stuff] The
constructor
function. This function contains all the logic to create an instance of the “class”, i.e. instance specific code . - [public stuff] The
prototype
object. This is the object the instance inherits from. It contains all methods (and other properties) that should be shared among all instances.
The
constructor
corresponds toprivate
, andprototype
corresponds topublic
in 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 instance
inherits from thePerson prototype
object. But it also implies that every dog inherits from one specificPerson
instance. 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
Person
instance specific code at that moment, we only want all the methods from thePerson prototype
object. That is whatObject.create
lets us do
prototype constructor correct
1 | /* parent object: Person */ |
II. prototype
vs __proto__
in JS, everything is an
object
except primitive value. objects can serve 2 goals:prototype
/class
for othersub-object
/sub-class
. In this case, we can checkprototype
of aconstructor
‘s shared parent object [ie. prototype]sub-object
/sub-class
of otherprototype
/class
- NOTE:
- an object created by
new
(constructor) doesn’t hasprototype
property, since it’s created with the hope to get an pure object! - If this object wants to have
prototype
property, 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 attachprototype
to 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
constructor
of this object - other shared
methods
orfields
of itsprototype
- the
- An
prototype
will always have aconstructor
property, to tell its corresponding U associated to this ‘class’ prototype
- an object created by
prototype
is 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