我有一个原型Node
,我为它创建了几个对象。
在这些对象的生存期内,I可能需要它们成为ValueNode
或PropertyNode
。我目前通过为每个"子类"使用一个助手来处理这个问题,并在两个助手上共享一个公共接口。想象一种状态模式。
然而,我想改进这个设计,通过使用传统功能扩展现有对象,而不使用助手。
即:
n = new Node();
...
// n needs to become a ValueNode
// ???
n.methodDefinedForValueNodesOnly();
这在javascript中可能吗?这是"良好实践"吗?
在阅读了这篇关于mixin的文章后,我最终使用了以下解决方案(它基本上很好地使用了mixin)。
Node = function() {};
Node.prototype.one = function() {alert(1)};
asValueNode = (function() {
function two() {
alert(2)
};
return function() {
this.two = two;
return this;
}
})();
u = new Node();
// u is a usable Node.
// ...
// Make u a ValueNode
asValueNode.call(u);
u.one();
u.two();
在JavaScript中,只需执行一次原型继承。您可以使用一些提供丰富类子系统的框架,如ExtJS、Ember.js等。另一种方法可以是迭代所需对象的属性,然后应用于目标对象。类似这样的东西:
function Node( desc ) {
this.desc = desc;
this.doNodeThing = function() {
console.log( "noding for " + this.desc );
}
}
function FooNode( desc ) {
this.desc = desc;
this.doFooNodeThing = function() {
console.log( "foo noding for " + this.desc );
}
}
function BarNode( desc ) {
this.desc = desc;
this.doBarNodeThing = function() {
console.log( "bar noding for " + this.desc );
}
}
function inherit( obj, superObj ) {
for ( var x in superObj ) {
if ( typeof superObj[x] == "function" ) {
obj[x] = superObj[x];
}
}
}
var n1 = new Node( "tree node" );
n1.doNodeThing();
var n2 = new Node( "folder node" );
n2.doNodeThing();
inherit( n1, new BarNode() );
n1.doBarNodeThing();
//n2.doBarNodeThing(); <= TypeError: n2.doBarNodeThing is not a function
inherit( n1, new FooNode() );
n1.doBarNodeThing();
n1.doFooNodeThing();
//n2.doFooNodeThing(); <= TypeError: n2.doFooNodeThing is not a function
上面的代码将向对象本身添加函数,而不是向其原型添加函数。
jsFiddle:http://jsfiddle.net/davidbuzatto/3cCGC/