The object create() method is used to create a new object with a specified prototype object (using an existing object) and properties.
The primary purpose of using this method is used for implementing inheritance.
Syntax
Object.create(prototype_object, propertiesObject)
Parameters
- prototypeObject: Newly created objects prototype object. It has to be an object or null .
- propertiesObject(optional): If specified, it’s an object whose own enumerable properties specify property descriptors to be added to the newly-created object.
Return Value
Returns a new object with the specified prototype object and properties.
Example 1: How to Use Object.create() method
let animalPrototype = {
speak() {
console.log(`The ${this.type} says ${this.sound}`);
}
};
let animal = Object.create(animalPrototype);
animal.type = 'lion';
animal.sound = 'roar';
animal.speak();
Output
The lion says roar
Example 2: Create Objects Without Prototype
nullObj = Object.create(null);
console.log(typeof(nullObj));
console.log(nullObj);
Output
object
[Object: null prototype] {}
Example 3: Adding Properties at Creation Time
let obj = Object.create(
{},
{
p: {
value: 21,
writable: true,
enumerable: true,
configurable: true,
},
},
);
console.log(obj)
Output
{ p: 21 }
Example 4: TypeError: Object prototype may only be an Object or null
If the prototypeObject parameter is neither null nor an object, it will throw a TypeError.
const obj = Object.create('hello', {
name: {
value: 'Ronaldo',
writable: true,
enumerable: true,
configurable: true
}
});
console.log(obj.name);
Output
TypeError: Object prototype may only be an Object or null: hello
at Function.create (<anonymous>)
at Object.<anonymous> (/Users/ankitlathiya/Desktop/code/app.js:1:20)
at Module._compile (node:internal/modules/cjs/loader:1255:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
at Module.load (node:internal/modules/cjs/loader:1113:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47
Solution
In the solution, the first argument of Object.create() is an empty object {}, which is a valid prototype. As a result, obj.name will correctly output ‘Ronaldo’.
const obj = Object.create({}, {
name: {
value: 'Ronaldo',
writable: true,
enumerable: true,
configurable: true
}
});
console.log(obj.name);
Output
Ronaldo
Browser Compatibility
- Chrome 5 and above
- Edge 12 and above
- Firefox 4 and above
- Opera 11.6 and above
- Safari 5 and above
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.