Introduction
Object in JavaScript
- var student = "Alice";
We can create a JavaScript object in one of the following three ways:
- Object Literal
- New Operator
- Object Constructor
Object Literal
We can create n JavaScript object literal, in this way we both define and create the object in one statement as shown in the following code snippet. It's an easiest way to create an object.
- var student = {
- name: "Sandeep",
- city: "jaipur",
- show: function ()
- {
- return this.name +" "+ this.city;
- }
- }
- The first one is using the dot operator (.), for example:
console.log(student.name); - Another way is using the property name as the index such as:console.log(student["city"]);
New Operator
We can create a JavaScript object using the new operator. It creates a new object. The type of this object, is simply Object. The following code snippet shows how to create a student object with its properties and methods. Both properties and methods are accessed in the same way as in the previous one.
- var student = new Object();
- student.name = "Alice";
- student.age = 18;
- student.getDetail = function ()
- {
- return this.name + " " + this.age;
- }
Object Constructor
Let's use “student” as an example. It has two properties and one method. One property is the name whereas another is age and the method name is getDetail(). The important thing to realize is that every student will have a different name, color and detail as well. To create an object type that accommodates this need for flexibility, we will use an object constructor.
- function student(name, age)
- {
- this.name = name;
- this.age = age;
- this.getDetail = function ()
- {
- return this.name + " " + this.age;
- }
- }
Objects defined using an object constructor will be instantiated using the new keyword. Actually the object constructor creates a blueprint for the objects, not the object itself. So you can create multiple objects of one type as shown in the following code snippet. Here we create two objects of the “student” type and both have different information.
- var student1 = new student("Alice", 22);
- console.log(student1.getDetail());//"Alice 22"
- var student2 = new student("Jon", 20);
- console.log(student2.getDetail()); // "Jon 20"
You can also add methods to an object using the prototype that the object creates by object constructor. A prototype is a type of inheritance in JavaScript. We use it when we would like an object to inherit a method after it has been defined, in other words we are attaching a method to an object after it's been defined. This method will be shared among all object instances. Let's extend the “student” type and add a new method that changes the student name after it's instantiated.
- function student(name, age)
- {
- this.name = name;
- this.age = age;
- this.getDetail = function ()
- {
- return this.name + " " + this.age;
- }
- }
- student.prototype.changeName = function (name)
- {
- this.name = name;
- }
- var student1 = new student("Alice", 22);
- console.log(student1.getDetail());//"Alice 22"
- student1.changeName("Mark");
- console.log(student1.getDetail());//"Mark 22"
The keyword prototype is always used after the object name. The prototype method is called the same as other methods using the dot operator. As in the preceding example, the student's name has been changed after the object was instantiated.
You can also define all the prototype methods in one container as shown in the following example.
- function student(name, age)
- {
- this.name = name;
- this.age = age;
- }
- student.prototype =
- {
- changeName: function (name)
- {
- this.name = name;
- },
- getDetail: function ()
- {
- return this.name + " " + this.age;
- }
- }
- var student1 = new student("Alice", 22);
- console.log(student1.getDetail());//"Alice 22"
- student1.changeName("Mark");
- console.log(student1.getDetail());//"Mark 22"

Rasmiranjan sahooPosted Apr 24, 2015, 6:34 AM
Well explanation
Rahul Kumar SaxenaPosted Apr 14, 2015, 2:23 PM
Good Show...
Ratnesh SinghPosted Apr 14, 2015, 3:29 AM
good one..
Sibeesh VenuPosted Apr 14, 2015, 1:31 AM
Nice one.
Karthik Muthu KaruppanPosted Apr 13, 2015, 1:32 PM
Good one
Nitesh KejriwalPosted Apr 13, 2015, 11:40 AM
good one Sandeep Singh Shekhawat
Santhakumar MunuswamyPosted Apr 13, 2015, 11:09 AM
Thanks for good one
NitinPosted Apr 13, 2015, 8:13 AM
Good one
Shakti Singh DulawatPosted Apr 13, 2015, 7:06 AM
very well Written
Gowtham RajamanickamPosted Apr 13, 2015, 6:02 AM
good one