Classes and this
Prototypes and instanceof
An object that another object "borrows" the properties and methods it doesn't have itself from.
Why it exists
If each of a thousand objects kept its own copy of every method, memory would run out fast, and fixing a method for everyone at once would be impossible. A prototype solves this: a method is stored in one place, and objects refer to it. That's why every array has map and push even though you never put them there: they live in Array.prototype. And that's why understanding prototypes explains where objects' methods come from and why this works in them.
Where it's used
- Class methods: everything declared in a class body goes into
Class.prototype. - Built-in methods:
[].mapcomes fromArray.prototype,"cat".toUpperCasefromString.prototype. - Checking an object's type:
error instanceof ValidationError,value instanceof Date,x instanceof Map. - Older code and libraries from before 2015:
function User() {}andUser.prototype.save = function () { … }— how classes were written before class.
Syntax
Object.getPrototypeOf(obj); // the object's prototype
Class.prototype; // where the class's methods live
obj instanceof Class; // true / false
Object.create(proto); // a new object with prototype protoExamples
class Robot {
hello() {
return "Beep";
}
}
const a = new Robot();
const b = new Robot();
a.hello === b.hello; // → true — one method for all
Object.getPrototypeOf(a) === Robot.prototype; // → true
a instanceof Robot; // → true
Object.keys(a); // → [] — no own properties, hello lives in the prototypeconst base = {
legs: 4,
describe() {
return "Legs: " + this.legs;
},
};
const bird = Object.create(base);
bird.legs = 2;
bird.describe(); // → "Legs: 2"
base.describe(); // → "Legs: 4"
"describe" in bird; // → true
Object.hasOwn(bird, "describe"); // → falsethis is the object it was called onfunction OldRobot(name) {
this.name = name;
}
OldRobot.prototype.hello = function () {
return "I am " + this.name;
};
new OldRobot("Bolt").hello(); // → "I am Bolt"
class Cat {}
typeof Cat; // → "function"
[] instanceof Array; // → true
[] instanceof Object; // → trueclass — and how a class is still a function under the hoodResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
When you read obj.prop, JavaScript looks for prop on the object itself. Not there — it checks its prototype, then the prototype's prototype, and so on up to Object.prototype, which has no prototype (null). That's the prototype chain. Found — return it; reached the end — undefined.
new Robot() makes Robot.prototype the prototype of the new object. class Cat extends Animal makes Animal.prototype the prototype of Cat.prototype. You get a chain: cat → Cat.prototype → Animal.prototype → Object.prototype → null.
Importantly, this doesn't change along the way. The method was found in the prototype, but it was called as obj.method() — so this is obj. That's how a shared method works with a particular instance's data.
obj instanceof Robot simply walks obj's chain and checks whether Robot.prototype shows up. This is a legacy of the Self language, where JavaScript got the idea: objects inherit not from classes but from other objects.