.create()
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.
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 prototypevar prototypeObject = {greet: function () {return 'Hello!';},};// Creating a new object with 'prototypeObject' as its prototypevar 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
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours