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?