shawnbot / custom-elements

All about HTML Custom Elements
Creative Commons Zero v1.0 Universal
201 stars 11 forks source link

V1 as an Object (without class) #7

Closed noopole closed 8 years ago

noopole commented 8 years ago

You wrote about v1:

As with the v0 API, the ElementClass can be [...] an object with a prototype that extends the base class's.

Do you know how to define shuch a custom element (without the class notation) ?

I've tried but Chrome raise an error and wants a functionas the second parameter. Exemple:

customElements.define( "object-v1", { prototype: Object.create( HTMLElement.prototype ) } )

noopole commented 8 years ago

I've found a way after all:

var CEo = function ()
{
    console.log( "created" )
    return Reflect.construct( HTMLElement, [], CEo )
}

CEo.prototype = Object.create( HTMLElement.prototype )

CEo.prototype.connectedCallback = function ()
{
    console.log( "connected" )
    this.innerHTML = "Hello v1"
} 

customElements.define( "object-v1", CEo )