【问题标题】:Polymer multiple inheritance/composition聚合物多重继承/组合
【发布时间】:2014-05-14 16:00:01
【问题描述】:

Polymer 网站says 不支持在 Polymer 中使用“扩展”属性进行多重继承(或组合)。我希望一个元素由一个 Polymer 元素的一些方法和另一个 Polymer 元素的一些方法组成,以使其反映应用程序逻辑。目前有什么方法可以在 Polymer 中实现它吗? (就像使用 javascript mixins 一样)

【问题讨论】:

  • 我也想知道不允许多重继承是因为实现复杂还是设计决定?

标签: javascript inheritance polymer web-component


【解决方案1】:

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

更多信息here

【讨论】:

    【解决方案2】:

    我无法说出 Polymer 人的推理,但通常认为它比 use composition over inheritance 更可取。

    【讨论】:

    • 谢谢,你是对的。但实际上我的意思是相同的,即 Polymer 中的组合机制是什么,但我使用强大的“基于类的继承”术语具有误导性。我会稍微修改一下这个问题,让它更清楚。
    • 我也想看看这个问题的答案。我建议发布另一个问题。
    【解决方案3】:

    Polymer 支持 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();
      }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2012-01-31
      • 2015-11-06
      • 2017-11-23
      • 1970-01-01
      • 2012-12-08
      • 2012-01-06
      相关资源
      最近更新 更多