|
|
|
|
|
by kenbellows
2881 days ago
|
|
Can you rephrase? The "prototype chain" refers to the whole sequence of objects that link to each other by the `__proto__` property, e.g.: let vertebrate = {hasSpine: true},
mammal = {hasHair: true},
dog = {sound: 'bark'};
sparky = {name: 'Sparky'},
jumbo = {name: 'Jumbo'};
mammal.__proto__ = vertebrate;
dog.__proto__ = mammal;
sparky.__proto__ = dog;
jumbo.__proto__ = dog;
console.log(sparky.hasSpine); // true
console.log(jumbo.hasHair); // true
console.log(sparky.sound); // 'bark'
console.log(jumbo.name); // 'Jumbo'
When you access `sparky.hasSpine`, the JS engine first checks whether `sparky` has an "own property" called `hasSpine`. It doesn't, so it checks `sparky.__proto__.hasSpine`, which is the same as `dog.hasSpine`. No luck, so it checks `sparky.__proto__.__proto__.hasSpine` (i.e. `mammal.hasSpine`), and finally `sparky.__proto__.__proto__.__proto__.hasSpine` (i.e. `vertebrate.hasSpine`), which resolves to `true`.This entire structure of objects linked through their `__proto__` property is what we call the "prototype chain". Does that answer your question? |
|