【发布时间】:2018-01-10 07:33:18
【问题描述】:
在构建 Polymer 2.0 ES6 Web 组件时,经常使用以下模式。
constructor() {
super();
}
Here the documentation describes calling the super(); function when defining an element.
However here in the Shop App 模式仅被遵循 3 次,在以下元素中:shop-app.html、shop-ripple-container.html、shop-tabs-overlay.html。
我们什么时候需要打电话给super()?这个调用什么时候需要在constructor() 函数中?没有像the Shop app 那样调用super() 的后果是什么?
编辑:
一位用户 (@4castle) 提出了这个问题might be a duplicate of this question。我恭敬地不同意。这个问题涉及 Polymer,而另一个问题涉及 React。另一个问题询问传递给super() 函数的参数。这个问题想知道当 super() 没有被调用时会发生什么,以及最好的调用位置在哪里(即在constructor() 内部与否)。
【问题讨论】:
-
super()当一个类extends另一个类时是必需的。否则,如果您尝试访问this而不先调用super(),它将抛出ReferenceError。 -
@4castle:我根据您的评论和您指出的其他问题添加并回答。我还编辑了这个问题来解释为什么我认为它不同。如果您不介意,请告诉我您是否认为我在正确的轨道上。
标签: javascript polymer polymer-2.x