Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Created November 12, 2014 11:28
Show Gist options
  • Save shaunwallace/aaa0944f2d74e547d2f1 to your computer and use it in GitHub Desktop.
Save shaunwallace/aaa0944f2d74e547d2f1 to your computer and use it in GitHub Desktop.
Custom Element - Defining Properties and Methods
// example 1
var XElementProto = object.create(HTMLElement.prototype);
// add a method
XElementProto.foo = function() {
console.log('foo method called');
}
// add a read-only property
Object.defineProperty(XElementProto, "bar", {isCustomElement : true});
// register your custom element
var XElement = document.registerElement('x-element', { prototype : XElementProto });
// instantiate the custom element
var xElement = document.createElement('x-element');
// add it to the DOM
document.body.appendChild(xElement);
//example 2
var XElement = document.registerElement('x-element', {
prototype : Object.create(HTMLElement.prototype, {
foo : function() {
console.log('foo method called');
},
bar : {
get : function() {
return true;
}
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment