Object.create( ) In JavaScript - GeeksforGeeks

13 Sep, 2021
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. 
Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.
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: 
It creates a fresh new object(instance) Automobile() and assigns it to a variable.It sets the constructor property i.e “color” of the object to Automobile.
It creates a fresh new object(instance) Automobile() and assigns it to a variable.
It sets the constructor property i.e “color” of the object to Automobile.
Object.create() Method Object.create() method 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:  
Object.create() is used for implementing inheritance.
Syntax:  
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.
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.  
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:
Javascript
<script>// creating a function which will be the//prototype for the object to be created laterfunction fruits() {    this.name = 'fruit 1';} // creating a function to whose object will// inherit properties from the prototype// using object.create() methodfunctionapple() {    fruits.call(this);} // creating an object of the apple function which// will have properties of the prototype// object i.e. fruitsapple.prototype = Object.create(fruits.prototype);const app = new apple(); // Displaying the created objectconsole.log(app.name);< /script>
OUTPUT : 
"fruit 1"
Code 2: 
Javascript
<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() methodfunction apple() {    fruits.call(this);} // creating an object of the apple function which// will have properties of the prototype// object i.e. fruitsapple.prototype    = Object.create(fruits.prototype);const app = new apple(); // Displaying the created objectconsole.log(app.name);console.log(app.season);< /script>
OUTPUT : 
"fruit 1"
"summer"
Exceptions :  
Object.create( ) method throws a TypeError exception if the propertiesObject parameter isn’t null.
Object.create( ) method throws a TypeError exception if the propertiesObject parameter is a non primitive object.
Supported Browsers: 
Google Chrome 6.0 and above
Internet Explorer 9.0 and above
Mozilla 4.0 and above
Opera 11.1 and above
Safari 5.0 and above
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create 
sumitgumber28
ysachin2314
javascript-functions
javascript-object
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
How to read a local text file using JavaScript?
Node.js | fs.writeFileSync() Method