JavaScript - create() method

revision:


Category : object

The Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.

Syntax :

        Object.create(proto)
        Object.create(proto, propertiesObject)
    

Parameters:

proto : required. The object which should be the prototype of the newly-created object.

propertiesObject : optional. If specified and not undefined, an object whose enumerable own properties specify property descriptors to be added to the newly-created object, with the corresponding property names. These properties correspond to the second argument of Object.defineProperties().

Examples:

        const person = {
            isHuman: false,
            printIntroduction: function() {
              console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
            }
          };
        const me = Object.create(person);
        me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"
        me.isHuman = true; // Inherited properties can be overwritten
        me.printIntroduction();
          // Expected output: "My name is Matthew. Am I human? true"
          
    

Practical examples

example: a new instance of an object is created

code:
                    <div>
                        <p id="create-1"></p>
                        
                    </div>
                    <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);
                        document.getElementById("create-1").innerHTML = "object created: " + app.name;
                        
                    </script>
                

example: a new instance of an object is created

code:
                    <div>
                        <p id="create-2"></p>
                        <p id="create-3"></p>
                    </div>
                    <script>
                        // creating a function which will be the prototype for the object to be created later
                        function fruits1() {
                            this.name = 'fruit 2';
                            this.season = 'summer';
                        }
                        // creating a function to whose object will inherit properties from the prototype using object.create() method
                        function apple1() {
                            fruits1.call(this);
                        }
                        // creating an object of the apple function which will have properties of the prototype object i.e. fruits
                        apple1.prototype = Object.create(fruits1.prototype);
                        const app1 = new apple1();
                        // Displaying the created object
                        console.log(app1.name);
                        console.log(app1.season);
                        document.getElementById("create-2").innerHTML = "object created: " + app1.name; 
                        document.getElementById("create-3").innerHTML = "object created: " + app1.season; 
                    </script>