Prototypal inheritance
Last updated
Was this helpful?
Last updated
Was this helpful?
So far we have seen some inheritance in action — we have seen how prototype chains work, and how members are inherited going up a chain. But mostly this has involved built-in browser functions. How do we create an object in JavaScript that inherits from another object?
First of all, make yourself a local copy of our file (see it also). Inside here you'll find the same Person()
constructor example that we've been using all the way through the module, with a slight difference — we've defined only the properties inside the constructor:
The methods are all defined on the constructor's prototype. For example:
Date
objects inherit from Date.prototype
Array
objects inherit from Array.prototype
Person
objects inherit from Person.prototype
The Object.prototype
is on the top of the prototype inheritance chain:
Date
objects, Array
objects, and Person
objects inherit from Object.prototype
.
Object.create
Object.setPrototypeOf
__proto__
Prototype
class syntax
copying fields