Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.create commit #4545

Merged
merged 13 commits into from
May 14, 2024
Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions content/javascript/concepts/objects/terms/create/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
Title: '.create()'
Description: 'Creates a new object with the specified prototype object and properties.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
- 'Web Development'
Tags:
- 'Algorithms'
- 'Inheritance'
- 'Methods'
- Objects'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

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

## Syntax

```pseudo
Object.create(prototype[, propertiesObject])
```

- `proto`: The object that will serve as the blueprint for the newly-created object.
- `propertiesObject`: An object whose properties will be added to the newly-created object. This parameter is optional.

## Example
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved

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

```js
// 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()); // Output: "Hello!"
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave a line after this, and also use the following after leaving the line:

It produces the following output:

Hello!


The code above produces the following output:

```shell
Hello!
```

In the provided 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.