InterviewHubPRO
Section Overview
Q207

Question 207 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

40%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

How do you create an object with a prototype

The Object.create() method is used to create a new object with the specified prototype object and properties. i.e, It uses an existing object as the prototype of the newly created object. It returns a new object with the specified prototype object and properties.

javascript
  const user = {
    name: "John",
    printInfo: function () {
      console.log(`My name is ${this.name}.`);
    },
  };

  const admin = Object.create(user);

  admin.name = "Nick"; // Remember that "name" is a property set on "admin" but not on "user" object

  admin.printInfo(); // My name is Nick