JavaScript .create()

Anonymous contributor's avatar
Anonymous contributor
Published May 14, 2024
Contribute to Docs

In JavaScript, the .create() method creates a new object with the specified prototype object and optional additional properties. It essentially allows developers to create an object that inherits properties from another object.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

Object.create(prototype[, propertiesObject])
  • prototype: The object that serves as the blueprint for the newly created object.
  • propertiesObject (Optional): The object whose properties are added to the newly created object.

Example

The following example explains the use of the .create() method:

// Creating an object to serve as the prototype
var prototypeObject = {
greet: function () {
return 'Hello!';
},
};
// Creating a new object with 'prototypeObject' as its prototype
var newObj = Object.create(prototypeObject);
// 'newObj' inherits the 'greet' function from 'prototypeObject'
console.log(newObj.greet());

The above code produces the following output:

Hello!

In the above example, an object named prototypeObject is created with a greet() method. Subsequently, .create() generates a new object newObj, inheriting the greet() method from prototypeObject. The invocation newObj.greet() then outputs Hello! to the console.

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours