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 all 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
53 changes: 53 additions & 0 deletions content/javascript/concepts/objects/terms/create/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
Title: '.create()'
Description: 'Creates a new object with the specified prototype object and properties.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Algorithms'
- 'Inheritance'
- 'Methods'
- 'Objects'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

In JavaScript, the **`.create()`** [method](https://www.codecademy.com/resources/docs/javascript/methods) 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

```pseudo
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
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());
```
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 above code produces the following output:

```shell
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.