FrontendDeveloper.in

JavaScript question detail

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.

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
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399