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(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.
In Textio, certain features are inherited based on the type of user (e.g., admins, moderators, regular users). They need a quick way to determine if a user has admin privileges or not.
Write a function isAdmin that takes an object and returns whether that object's prototype references the adminUser object.