【发布时间】:2011-12-14 15:56:55
【问题描述】:
可能重复:
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;
}
}
或
function car(){
this.engineOn = false;
}
car.prototype.startEngine = function(){
this.engineOn = true;
}
最好的方法是什么?这两种方法有什么好处或坏处吗?
【问题讨论】:
-
我认为您也打算在第一个中添加
this.engineOn = false;。因为现在两段代码不等价,因为第一段没有engineOn的起始值。
标签: javascript object