jav*_*ter 5 javascript extends prototype
我想知道动态扩展Function的原型是不好的做法.我正在考虑使用静态方法来执行此操作,该方法接收属性名称和要添加到原型的函数.
这是一种不好的做法吗?
function Test() {
this.condition = false;
}
Test.extend = function(id, task) {
this.prototype[id] = function() {
return task.apply(this, arguments);
};
};
Run Code Online (Sandbox Code Playgroud)
我想说在这种情况下这是不好的做法,因为您无法控制添加的 [id] 方法是否从类内被覆盖。
var test = new Test();
test.extend("example", function() {
console.log("First Method");
});
test.extend("example", function() {
console.log("Second Method");
});
Run Code Online (Sandbox Code Playgroud)
根据您的代码,您无法知道第一个方法何时被覆盖,从而随机破坏您的代码。
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |