

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 10
click for more info
Not enough gems
Cost: 6 gems
1: Prototypal Inheritance
incomplete
2: Prototype Chains
incomplete
This lesson's interactive features are locked, please to keep using them
Every object has a prototype, and that prototype can in turn have a prototype, creating a chain that goes all the way back to the root Object object, whose prototype is always null.
An object stores a reference to its prototype. The Object.getPrototypeOf() method returns the prototype of an object. When we create a new POJO (plain old JavaScript object), its prototype is automatically set to Object.prototype:
const pureTitan = {
name: "Eren's mom",
};
const beastTitan = Object.create(pureTitan);
beastTitan.name = "Zeke";
console.log(beastTitan); // { name: "Zeke" }
console.log(Object.getPrototypeOf(beastTitan)); // { name: "Eren's mom" }
console.log(Object.getPrototypeOf(beastTitan) === pureTitan); // true
console.log(Object.getPrototypeOf(Object.getPrototypeOf(beastTitan))); // {} (Object.prototype)
console.log(
Object.getPrototypeOf(
Object.getPrototypeOf(Object.getPrototypeOf(beastTitan)),
),
); // null (end of the chain)
You might think that using Object.create() copies the properties from the parent object to the child object:
const pureTitan = {
name: "Eren's mom",
};
const beastTitan = Object.create(pureTitan);
console.log(beastTitan.name); // Eren's mom
But it does not. JavaScript looks within the beastTitan object for the name property and doesn't find it because we never set one. So it checks its prototype (using Object.getPrototypeOf(beastTitan)), which is pureTitan, and finds the name property there. It uses that value instead.
Textio models admin users as objects whose immediate prototype is adminUser.
Write a function isAdmin that takes an object and returns whether that object's prototype references the adminUser object.