从custom elements一页,我看到来扩展你的元素:在引导后扩展HTML元素
var XFooButtonPrototype = Object.create(HTMLButtonElement.prototype);
XFooButtonPrototype.createdCallback = function() {
this.textContent = "I'm an x-foo button!";
};
var XFooButton = document.registerElement('x-foo-button', {
prototype: XFooButtonPrototype,
extends: 'button'
});
然后它说,你可以写:让一个元素:
<x-foo></x-foo>
或者:
<button is="x-foo-button"></button>
问题:
为什么很重要,指定
extends: 'button'
当元素为HTMLButtonElement
obviously_继承(因为它在它的原型链HTMLButtonElement.prototype
)如何
button
和x-foo-button
之间的联系建立?由于extends: 'button'
,x-foo-button
成为is="x-foo-button"
按钮的可能选项吗?可以说“内部”会发生什么?为什么要选
<button is="x-foo-button"></button>
超过<x-foo></x-foo>
......?
[附录]
聚合物救我们脱离这种重复:
MyInput = Polymer({
is: 'my-input',
extends: 'input',
created: function() {
this.style.border = '1px solid red';
}
});
如果extends
是存在的,聚合物就会把正确的原型链与Object.getPrototypeOf(document.createElement(tag));
。 所以,配套问题:
- 为什么重复在第一?如果有
extends
,浏览器不应该自动执行此操作吗?
我不认为双方你提到的语法是可以互换的。在_using一个自定义element_节wiki下指出: _如果您已经使用扩展来创建自 现有DOM元素(例如HTMLElement以外的其他元素)派生的自定义元素,请使用is syntax_ – Abhinav
Ta发布答案。感谢百万的评论! – Merc