Skip to content
Shubham JangleResources
JavaScript

Explore topics

WTF is __proto__? (Object Literal & Object with getter and setter)

Watch the walkthrough
Open on Instagram

Revision clarifications

Prototype lookup continues through the chain until a property is found or the prototype is null. Prefer Object.getPrototypeOf/Object.create to the legacy proto accessor. The circular assignment at the end intentionally throws; run the examples separately.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

/** Object Literal **/
const rohit = {
  girlfriend: "smita",
};

const vikas = {
  friend: "sakshi",
};

vikas.__proto__ = rohit; // we can do this bze __proto__ has get and set for __proto__ property
Object.setPrototypeOf(vikas, rohit); // newer way

console.log(vikas.friend);
console.log(vikas.girlfriend);
console.log(vikas); // gets rohit in __proto__ object

// NOTE: No circular dependencies are allowed. Uncaught TypeError: Cyclic __proto__ value
vikas.__proto__ = rohit;
rohit.__proto__ = vikas;
/** 2. Object with Getter and Setter **/
const men = {
  status: "single",
  crush: "yashashree.rao",
  set setCrush(dammm) {
    this.crush = dammm;
  },
};

const you = {};
Object.setPrototypeOf(you, men);
console.log(you.crush); // yashashree.rao
you.setCrush = "payalgamingg";
console.log(you.crush); // payalgamingg

// Get keys of an object
console.log(Object.keys(you)); // ["crush"]

// for...in loop includes inherited props
for (let key in you) console.log(key); // crush, status, setCrush