admin管理员组文章数量:1289866
It is possible to define custom functions on custom elements?
Something like:
var proto = Object.create(HTMLElement.prototype);
proto.customMethod = function () { ... };
document.registerElement('custom-el', {
prototype: proto
});
And calling the method on the element:
var istance = document.createElement('custom-el');
instance.customMethod();
It is possible to define custom functions on custom elements?
Something like:
var proto = Object.create(HTMLElement.prototype);
proto.customMethod = function () { ... };
document.registerElement('custom-el', {
prototype: proto
});
And calling the method on the element:
var istance = document.createElement('custom-el');
instance.customMethod();
Share
Improve this question
asked Sep 11, 2016 at 18:02
TheGr8_NikTheGr8_Nik
3,2004 gold badges20 silver badges37 bronze badges
1 Answer
Reset to default 10Yes, of course.
Your example works as you can see in the code snippet below:
New answer for Custom Elements v1
class CE extends HTMLElement {
customMethod() {
console.log( 'customMethod called' )
}
}
customElements.define( 'custom-el', CE )
var instance = document.createElement( 'custom-el' )
instance.customMethod()
Old answer for Custom Elements v0 (deprecated)
var proto = Object.create(HTMLElement.prototype);
proto.customMethod = function() {
console.log('customMethod called')
};
document.registerElement('custom-el', {
prototype: proto
});
var instance = document.createElement('custom-el');
instance.customMethod();
本文标签: javascriptCustom methods of Web componentsStack Overflow
版权声明:本文标题:javascript - Custom methods of Web components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443234a2379039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论