聚合物多重继承/组合

Polymer multiple inheritance/composition

本文关键字:组合 多重继承 聚合物      更新时间:2023-09-26

Polymer网站说多重继承(或组合)不支持使用'extend'属性在Polymer。我希望一个元素由来自一个聚合物元素的一些方法和来自另一个聚合物元素的一些方法组成,以反映应用程序逻辑。目前有任何方法来实现,在聚合物?(如使用javascript mixins)

Polymer现在支持mixin:

var mixinObj = {
   foo: function() {
     /* ... */
   }
};
var mixinObj2 = {
   foo2: function() {
     /* ... */
   }
};

Polymer('my-component', Polymer.mixin({   // Platform.mixin for polymer version < 0.5
  bar: function() {
     /* ... */
     this.foo();   // all the functions in mixinObjs are now accessible through 'this'   
     this.foo2();   

  }

}, mixinObj, mixObj2);  // Platform.mixin accepts multiple mixin objects

更多信息在这里

我不能说聚合物的人的理由,但它通常被认为是更可取的使用组合而不是继承。

聚合物支持Mixin概念以克服多重继承概念。

的例子:

Class ElementOne extends Polymer.Element {
   ready() {
     super.ready();   
   }
}
Class ElementTwo extends Polymer.Element {
  ready() {
     super.ready();
  }
}
Class ElementThree extends ElementOne(ElementTwo(Polymer.Element)) {
  ready() {
     super.ready();
  }
}

希望对你有帮助。