【发布时间】:2012-05-17 13:22:58
【问题描述】:
可能重复:
Advantages of using prototype, vs defining methods straight in the constructor?
在 JavaScript 中创建自定义类和公共方法时的最佳做法是什么,更重要的是……为什么?
使用 'this' 创建公共方法?
var myClass = function() {
this.myVar = "this is my value";
this.myFunc = function() {
alert( this.myVar );
};
this.myFunc();
};
-或- 使用“原型”创建公共方法?
var myClass = function() {
this.myFunc();
};
myClass.prototype = {
myVar: "this is my value",
myFunc: function() {
alert( this.myVar );
}
};
非常感谢!!!
【问题讨论】:
-
这个问题每天至少被问一次...
标签: javascript oop prototype