Object.create() method

The Object.create function serves the purpose of generating a new object, utilizing a designated prototype object along with its properties. Additionally, it is possible to instantiate an object devoid of a prototype by employing Object.create(null).

Syntax:

Example

Object.create(prototype[, propertiesObject])

Parameter

prototype: This refers to the prototype object that serves as the basis for creating a new object.

propertiesObject: This parameter is not mandatory. It defines the enumerable properties that will be included in the object that is being newly created.

Return

The method Object.create generates a fresh object that is associated with the designated prototype object and its properties.

Browser Support:

Chrome Yes
Edge Yes
Firefox Yes
Opera Yes

Example 1

Example

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

Output:

Output

"My name Marry. Am I human? true"

Example 2

Example

function fruits() {
        this.name = 'franco';
        }
       function fun() {
        fruits.call(this)
 }

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

Output:

Output

"franco"

Example 3

Example

function fruits() {
        this.name = 'fruit';
        this.season = 'Winter';
        }

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

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

Output:

Output

"fruit"
 "Winter"
"Winter"

Input Required

This code uses input(). Please provide values below: