Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Think of it as changing your family tree! Every object in JavaScript has a hidden link to another object (its prototype) - like a parent. With Object.getPrototypeOf() you can see who the parent is, and with Object.setPrototypeOf() you can change the parent at runtime!
Object.create() to set prototype during creation instead!Read the prototype - see what object inherits from
const proto = Object.getPrototypeOf(obj);Change the prototype - make object inherit from something else
Object.setPrototypeOf(obj, newProto);You might see obj.__proto__ in old code. It's the old way to access prototypes. Don't use it!Use the standard Object.getPrototypeOf() instead.
obj.__proto__Object.getPrototypeOf(obj)Inspect the prototype chain of objects
Dynamically change what an object inherits from
myObject[[Prototype]]Object.prototypenullSet the prototype when creating the object - fast and clean!
const obj = Object.create(protoObj);Changing prototypes repeatedly is extremely slow! JavaScript engines can't optimize it.
// BAD - Very slow!
for (let obj of objects) {
Object.setPrototypeOf(obj, newProto);
}Reading prototypes with getPrototypeOf() has no performance penalty. Use it freely for debugging and inspection!
Use Object.getPrototypeOf() to inspect
Use Object.setPrototypeOf() sparingly
Prefer Object.create() over changing prototypes
Objects inherit from parent until reaching null