【发布时间】:2014-08-29 00:41:58
【问题描述】:
我真的很喜欢John Resig's simple inheritance method。它有很好的语法,而且 this._super 非常强大。
2014 年很艰难,我希望能够定义 getter 和 setter 以及其他描述符(但尽可能保持 Resig 版本的简单性)。
在保持类似于我非常珍视的 Resig 的语法的同时,我将如何解决这个问题?
我的梦想是这样的:
var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
},
dance: function(){
return this.dancing;
}
tools: { // <---- this would be so awesome
get: function() { ... },
set: function(v) { ... },
enumerable: true
},
});
var Ninja = Person.extend({
init: function(){
this._super( false );
},
dance: function(){
// Call the inherited version of dance()
return this._super();
},
swingSword: function(){
return true;
},
tools: {
get: _super, // <---- and this too
set: function(v) {
this._super(v);
doSomethingElse();
}
}
});
【问题讨论】:
-
继承永远不是答案!尤其是在 javascript 中。
标签: javascript oop inheritance getter-setter prototypal-inheritance