VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-object-create-method/

⇱ JavaScript Object create() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript Object create() Method

Last Updated : 12 Jul, 2024

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

Syntax:

Object.create(prototype[, propertiesObject])

Parameters:

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

Output:

"fruit 1"

Example 2: In this example, there are two functions "fruits" and "apple".A new instance of apple is created which is named "app" and it has been specified with the prototype and property of "fruits" i.e. this.name = 'fruit 1' and this .season = 'summer'.

Output:

"fruit 1"
"summer"

Applications:

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

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.

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers:

  • Google Chrome
  • Mozilla
  • Opera
  • Safari
Comment