可能重复:
Should I use prototype or not?
Closures in auto executing functions vs objects
所以,我在JavaScript中创建一个对象,有两种方法可以解决这个问题:
function car(){
this.engineOn = false;
this.startEngine = function(){
this.engineOn = true;
}
}
OR
function car(){
this.engineOn = false;
}
car.prototype.startEngine = function(){
this.engineOn = true;
}
最好的方法是什么?这两种方法都有任何好处或缺点吗?
答案 0 :(得分:2)
是的,原型不是为每个对象创建的,而是为所有对象创建一次。