Possible Duplicate:
Should I use prototype or not?
Closures in auto executing functions vs objects
So, I’m creating an object in JavaScript, there are two ways of going about this:
function car(){
this.engineOn = false;
this.startEngine = function(){
this.engineOn = true;
}
}
OR
function car(){
this.engineOn = false;
}
car.prototype.startEngine = function(){
this.engineOn = true;
}
What is the best way to do this? And are there any benefits or disadvantages to either method?
yes, prototype is not created with each object, but is created once for all objects.