This file covers how JavaScript objects relate to each other and how inheritance works under the hood.
JavaScript uses prototype-based inheritance. Classes are syntax on top of prototypes, not a separate inheritance system.
- Objects hold properties and methods.
- Property lookup checks the object first, then walks the prototype chain.
- Methods are just function-valued properties.
If a property is not found on the current object, JavaScript looks up the chain through the object's prototype.
const animal = {
speak() {
return "sound";
},
};
const dog = Object.create(animal);
dog.bark = function () {
return "woof";
};
dog.speak(); // inherited from animal- Thinking methods are copied into every instance automatically.
- Confusing prototype inheritance with class-based inheritance from other languages.
- Before classes, constructor functions plus prototypes were the common pattern.
newcreates an object, links it to the function's prototype, and bindsthis.
function User(name) {
this.name = name;
}
User.prototype.sayHi = function () {
return `Hi, ${this.name}`;
};Classes provide cleaner syntax, but the runtime model is still prototype-based.
class User {
constructor(name) {
this.name = name;
}
sayHi() {
return `Hi, ${this.name}`;
}
}JavaScript classes are syntactic sugar over prototype-based inheritance. They improve readability, but the underlying lookup model is still the prototype chain.
Object.createcreates an object linked directly to another object.- OLOO means objects linked to other objects.
- It is a direct prototype-based style that avoids pretending JavaScript is class-based.
The chain JavaScript walks when a property is missing on the current object.
No. They are syntax over the prototype system.
It creates an object, links it to the prototype, binds this, and returns the new object unless another object is returned explicitly.