Tuesday, September 17, 2013

What is the difference between the following two declarations and when should they be used

What is the difference between the following two declarations and when
should they be used

I am learning javascript. I am confused between the following two
notations where the newly created objects inherit the properties. How are
they different and when should i use them?
Code 1:
var Vehicle = function Vehicle() {
this.wheel=2
}
var vehicle = new Vehicle();
alert(vehicle.wheel);
Code 2:
var Vehicle = function Vehicle() {
}
Vehicle.prototype.wheel = 4;
var vehicle = new Vehicle();
alert(vehicle.wheel);
When is the keyword prototype used?

No comments:

Post a Comment