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.

  • 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:

  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:

  • Object.create() is used for implementing inheritance.

Syntax:

Object.create(prototype[, propertiesObject])

Parameters Used:

  1. prototype : It is the prototype object from which a new object has to be created.
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


OUTPUT :

"fruit 1"

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.