In JavaScript we can create objects in many ways, some of them are listed below:
We can create objects by using new keyword and we can assign a value to the object while creating in or we can also set the various properties of that object which can be accessed later on by putting dot after the object name it self.
Ex. (1)
var obj = new Object("myObj");
Ex. (2)
var obj = new Object();
obj.name = 'Gaurav';
obj.work= 'Study';
obj.subject= 'Computer Science';
We can also create Objects with an object literal such as :
var house= {type:"Duplex", area:"500", Height:"600"};
We can also create Objects like a function such as :
var obj = function () {
name:'Gaurav';
work: 'study';
subject: 'Computer Science';
}
or
function employee(name, emailId, contactNo) {
this.fullName= name;
this.email= emailId;
this.contact= contactNo;
}
var employee1= new employee("Mark wood", "mk@xyz.com", "123456");
var employee2= new employee("Joy Root", "jr@xyz.com", "789012");
0 Comment(s)