We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Prototype Chains

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)

How Are Parent Members Accessed?

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.

Assignment

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.