The Wayback Machine - https://web.archive.org/web/20210903210548/https://www.geeksforgeeks.org/object-create-javascript/amp/

Object.create( ) In JavaScript

Object and Object Constructors in JavaScript?
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.

For instance, consider the following code:

function Automobile(color) {
  this.color=color;
}

var vehicle1 = new Automobile ("red");

The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”.

When new Automobile() is called, JavaScript does two things:

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.create() Method
Object.create() methord is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties.



Applications:

Syntax:

Object.create(prototype[, propertiesObject])

Parameters Used:

  1. prototype :
  2. It is the prototype object from which a new object has to be created.

  3. 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.

Examples of the above function are provided below.

Examples:



Input : function fruits() {
        this.name = 'fruit 1';
        }

        function apple() {
        fruits.call(this);
        }

        apple.prototype = Object.create(fruits.prototype);
        const app = new apple();
        console.log(app.name);

Output : "fruit 1"

Explanation: In this example, there are two functions “fruits” and “apple”.A new instance of apple is created which is named as “app” and it has been specified with the prototype and property of “fruits” i.e. this.name = ‘fruit 1’.

Input : function fruits() {
        this.name = 'fruit 1';
        this.season = 'summer';
        }

        function apple() {
        fruits.call(this);
        }

        apple.prototype = Object.create(fruits.prototype);
        const app = new apple();
        console.log(app.name);
        console.log(app.season);

Output : "fruit 1"
         "summer"

Explanation: In this example, there are two functions “fruits” and “apple”.A new instance of apple is created which is named as “app” and it has been specified with the prototype and property of “fruits” i.e. this.name = ‘fruit 1’ and this .season = ‘summer’.

Codes for the above function are provided below.

Code 1:




<script>
// 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);
< /script>

OUTPUT :

"fruit 1"

Code 2:




<script>
    // creating a function which will be the
    // prototype for the object to be created later
    function fruits() {
        this.name = 'fruit 1';
        this.season = 'summer';
    }
  
// 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);
console.log(app.season);
< /script>

OUTPUT :

"fruit 1"
"summer"

Exceptions :

Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create




Article Tags :