JavaScript object creation process can be done by following ways.
Wrap every object property inside the curly braces { }
and separate every key-value pair with a comma(,)
. Please keep in mind that, property refers to key-value where property key name and value is separated by colon(:)
To create an object using object literal, follow the following rule.
{}
:
as a separator between the property name(key) and value.(,)
to separate every javascript property. Property refers to object key and its value. Source Code
var person = {
name: "John Doe", //name Property
age: 50, //age Property
job:"Developer" //job Property
getInfo : function () {
console.log(this.name + ' ' + this.job);
}
};
person.getInfo();
//output: John Doe Developer
Javascript object can be created using the constructor function in the following ways:
When the javascript constructor function is invoked by a new operator then it creates a new instance of the object and returns it.
Javascript provides various built-in object constructor functions. That is used to create a new object.
The built-in object constructor function when invoked by a new operator then it creates a new object and returns us then we can add multiple properties and methods to it.
Here, we are using the built-in object constructor function:Object
to create a new object.
Source Code
const userInfo = new Object();
userInfo.name = "John Doe";
userInfo.job = "Web developer";
userInfo.getInfo = function () {
console.log(this.name + " " + this.job);
};
userInfo.getInfo(); //John Doe
Follow the following steps to create an object using a user-defined constructor function:
this
.Let us understand it with the help of an example.
Source Code
//Constructor function
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.fullName = function () {
return this.name + " " + this.age + " " + this.job;
};
}
//Create a as many objects as you want
let p1 = new Person("smith", 28, "developer");
let p2 = new Person("John Doe", 30, "designer");
The javascript Object.create()
method is used to create a new object, using an existing object as the prototype of the newly created object. We pass the Prototype as the first argument to the Object.create()
method.
The following code creates a new object p and sets the person as to its prototype.
Source Code
const person = {
getInfo: function () {
document.write("My name is " + this.name + " and my age is " + this.age);
},
};
const p = Object.create(person);
p.name = "John Doe";
p.age = 32;
p.getInfo();
The Second argument to object.create
is a property descriptor.
Source Code
const person = {
getInfo: function () {
document.write("My name is " + this.name + " and my age is" + this.age);
},
};
descriptors = { name: { value: "John Doe" }, age: { value: 32 } };
const p = Object.create(person, descriptors);
p.getInfo();
The Object.assign()
method copies all enumerable own properties from one or more source objects to a target object. Objects are assigned and copied by reference. It will return the target object.
Source Code
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 4, c: 5 };
const returnedTarget = Object.assign(obj1, obj2);
console.log(obj1);
console.log(returnedTarget);
The object is basically the instance of the class. So, to create an object through class, use a new
operator.
Source Code
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
// Method
getInfo() {
return this.name + " " + this.age;
}
}
const person = new Person("Smith", 32);
console.log(person.getInfo()); // Smith 32