Sub topic 2
Shadowing: Writes Go to the Object, Not the Prototype
This chapter assumes you know object literals, how this is bound in method calls, and how Object.create and Object.getPrototypeOf work.
Shadowing and Own Properties
Reading a property walks up the prototype chain. Assigning a property does not. Assignment creates an own property on the target object, and that property shadows (hides) the prototype's value with the same name.
const proto = { count: 0 };
const a = Object.create(proto);
a.count = 5; // own property on a
console.log(proto.count); // 0
console.log(Object.hasOwn(a, 'count')); // true
console.log(Object.hasOwn(a, 'toString')); // false
console.log('toString' in a); // true (inherited)
for (const key in a) console.log(key); // "count", listed once| Check | Own | Inherited |
|---|---|---|
| `Object.hasOwn(obj, k)` | yes | no |
| `k in obj` | yes | yes |
| `for...in` | enumerable | enumerable |
Constructors, new, and constructor
Every ordinary function has a prototype property. That property is not the function's own [[Prototype]] link. It is the object that instances created with new will link to.
When you call new Person('Ada'), JavaScript does four things:
- It creates an empty object.
- It sets the object's
[[Prototype]]toPerson.prototype. - It runs
Personwiththisbound to the new object. - It returns the new object, unless the function returns some other object.
Person.prototype.constructor points back to Person by default.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hi, I'm ${this.name}`;
};
Person.prototype.tags = []; // shared by every instance
const ada = new Person('Ada');
const bob = new Person('Bob');
console.log(ada.greet()); // "Hi, I'm Ada"
console.log(Person.prototype.constructor === Person); // true
ada.tags.push('admin'); // mutates the shared array
console.log(bob.tags); // ['admin']Inheritance Before Classes
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
function Dog(name) {
Animal.call(this, name); // parent sets up this instance
}
Dog.prototype = Object.create(Animal.prototype);
Object.defineProperty(Dog.prototype, 'constructor', {
value: Dog, writable: true, configurable: true, enumerable: false
});
const rex = new Dog('Rex');
console.log(rex.speak()); // "Rex makes a sound"
console.log(rex.constructor === Dog); // trueReplacing Dog.prototype throws away the original constructor property. You restore it as non-enumerable so that it behaves like the default one.
Exercise
After const a = Object.create({ n: 1 }); a.n++;, what are a.n and the prototype's n?
Show answer
a.n is 2 and the prototype's n stays 1. a.n++ reads the inherited value 1, then assigns 2 as a new own property on a, which shadows the prototype's value.
Exercise
What goes wrong if you write Dog.prototype = Animal.prototype instead of using Object.create?
Show answer
Both constructors then share one prototype object. Adding methods to Dog.prototype would change Animal instances too.