【发布时间】:2013-03-22 21:20:40
【问题描述】:
这是我的问题,我有一个对象 Car 和他们的属性,我在对象内部定义了一个方法。但我认为建议将此方法附加到对象原型中,但为什么呢?有什么优点和缺点?谢谢你:)
我做了什么..
function Car (desc) {
this.desc = desc;
this.color = "red";
this.getInfo = function getInfo() {
return 'A ' + this.color + ' ' + this.desc + '.';
};
}
推荐:
Car.prototype.getInfo = function() {
return 'A ' + this.color + ' ' + this.desc + '.';
};
【问题讨论】:
-
@bfavaretto 不,我认为这更像是他正在寻找的东西:stackoverflow.com/questions/9772307/…
-
非常相关,但又不同。 @SethenMaleno 有它。
标签: javascript jquery prototype