Prototype chain là đường JavaScript đi tìm thuộc tính.
Nếu obj.name không có trên obj, JS tìm tiếp trên prototype... cho tới khi gặp null thì dừng.
javascript
const animal = { breathes: true };
const dog = Object.create(animal);
dog.bark = true;
console.log(dog.bark); // true — own property
console.log(dog.breathes); // true — inherited from animal
console.log(dog.__proto__ === animal); // true
// hasOwnProperty kiểm tra chỉ own property:
console.log(dog.hasOwnProperty('breathes')); // falseThe prototype chain is the path JavaScript follows to find a property.
If obj.name is not on obj, JS checks the prototype, and so on until null.
javascript
const animal = { breathes: true };
const dog = Object.create(animal);
dog.bark = true;
console.log(dog.bark); // true — own property
console.log(dog.breathes); // true — inherited from animal
console.log(dog.__proto__ === animal); // true
// hasOwnProperty only checks own properties:
console.log(dog.hasOwnProperty('breathes')); // false