Object.create vs. New in javascript

Jallen Liao
2 min readJul 8, 2020

Object.create() creates a new object, using an existing object as the prototype of the newly created object. This method is mainly used for implementing inheritance.

Object.create(prototype[, propertiesObject])

Parameters Used:

  • prototype : It is the prototype object from which a new object has to be created.
  • propertiesObject : It is optional parameter. It specifies the enumerable properties to be added to the newly created object.

Return Value:
Object.create() returns a new object with the specified prototype object and properties.

// creating a function which will be the 
//prototype for the object to be created later
function fruits() {
this.name = 'fruit 1';
}
// creating a function to whose object will
// inherit properties from the prototype
// using object.create() method
function
apple() {
fruits.call(this);
}
// creating an object of the apple function which
// will have properties of the prototype
// object i.e. fruits
apple.prototype = Object.create(fruits.prototype);
const app = new apple();
// Displaying the created object
console.log(app.name); // fruit 1
var fruit = Object.create(fruits);console.log(fruit.name1); // undefinedvar fruit1 = new fruits();
console.log(fruit1.name1); // fruit 1

Object.create and constructor function new both create a new object and both set the __proto__ but in a different way. Object.create sets the __proto__ to be the one passed as the first parameter. The constructor function set the __proto__ to the type specified prototype. The major difference is that Object.Create returns the new object while the constructor function return the constructor of the object or the object.

This is due to the important difference that new actually runs constructor code, whereas Object.create will not execute the constructor code.

--

--