Objects in JavaScript

Objects in JavaScript

In JavaScript, there are many ways to create objects.

1- Using object literal syntax. 2- Using the 'new' keyword 3- Using object.create() 4- Using object.assign()

In this article we will talk about object.create().

First, what is the object.create(): It's a new way to create objects in JavaScript, this method uses an existing object to create new objects.

this existing object act as a prototype for the newly created objects.


//prototype object

let originalObj = { 
    age: 23,
    city: 'Cairo',
};


let Lucaa = object.create(originalObj);
- create(): 
// method accept a parameter which is a prototype object and based on that prototype object will create a new object. 

//prototype will be "originalObj"

This will create an empty object and this empty object inherits the properties and methods of this originalObj.

![empty object with prototype](cons.PNG)

<br>

Now, this new object does not have a property or method of its own but it inheriting everything of originalObj.

Here we created an empty object but we can add some properties or methods to this object as a normal way we do this.

Example.

Lucaa.name = 'Lucaa';
Lucia.birth = '18/8/2008';
Now we have added two properties to this object and if we console.log(Lucia) again we should have in this object a **name**, and **birth** as properties to this new object.
![object with its own properties](con2.PNG).
after that, we can pass the *properties* which we want to this object as the second parameter to this object.create() method and the second parameter is an **optional parameter**.

let Tom = object.create(originalObj, {
    name: {value: 'Tom'},
    birth: {value: 19/9/1999}
})
<br/>
#####Object.create(): method accepts two arguments:

- The first parameter is the **prototypeObject**.

- The second parameter is the **properties** of that object and the second parameter is an optional parameter.
Finally, I hope you understand how to create an object with create() method in JavaScript and know how it works. Thanks for your time for reading this article. and if you have any questions you reached me on [Twitter](https://twitter.com/tahamahmoudx417).