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();
Yes, 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.