0

我有一个原型Node,我创建了几个对象。

在这些对象的生命周期中,我可能需要它们成为ValueNodeor PropertyNode。我目前通过为每个“子类”使用一个助手来处理这个问题,并在两个助手上共享一个 commong 接口。想想像状态模式这样的东西。

但是,我想改进这个设计,通过使用附加功能实际扩展现有对象,而不是使用帮助程序。

IE:

n = new Node();

...

// n needs to become a ValueNode
// ???

n.methodDefinedForValueNodesOnly();

这在javascript中可能吗?这是“好习惯”吗?

4

2 回答 2

1

在阅读了这篇关于 mixins 的文章后,我最终使用了以下解决方案(基本上很好地使用了 mixins)。

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();
于 2012-08-29T16:27:29.160 回答
0

在 JavaScript 中,您可以只执行一次原型继承。您可以使用一些提供丰富类子系统的框架,例如ExtJSEmber.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/

于 2012-08-29T16:02:24.833 回答