Created
November 12, 2014 11:28
-
-
Save shaunwallace/aaa0944f2d74e547d2f1 to your computer and use it in GitHub Desktop.
Custom Element - Defining Properties and Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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