Re: alternate view on constructors for custom elements

> On Jul 17, 2015, at 1:14 PM, Travis Leithead <Travis.Leithead@microsoft.com> wrote:
> 
> From: Domenic Denicola [mailto:d@domenic.me] 
> 
>>> window.XFoo = document.registerElement(‘x-foo’, XFooStartup);
>> 
>> Why is XFoo different from XFooStartup? If I define a method in XFooStartup, does it exist in XFoo?
> 
> This won't work as I described it, given what you've told me, but my assumption was essentially, that XFooStartup would act as if it didn't really depend on HTMLElement for construction. So, it's constructor wouldn't be linked to the actual custom element creation. Therefore XFoo (the platform-provided constructor function is the thing that is actually used to trigger construction, which would then result in the XFooStartup constructor running. Basically, like this (reverting to non-class syntax):
> 
> function XFooStartup(val1, val2) {
>      this.prop = val1;
>      this.prop2 = val2;
> }
> window.XFoo = document.registerElement(‘x-foo’, XFooStartup);
> 
> all I was trying to express different from the current design is: 
> 1) replacing the createdCallback with the function constructor (and passing the new element instance as 'this' when calling it)
> 2) passing through params from the XFoo platform-native constructor to the XFooStartup function
> 3) calling XFooStartup synchronously

We can do this without wrapping author supplied constructor.  In ES6/ES2015 classes, `this` variable is in the temporary dead zone (TDZ) until `super()` which allocates `this` and any attempt to access it will throw `ReferenceError`.  In other words, XFooStartup has no way of accessing the newly constructed object until `super()` has returned.  This in turns allows browser engines to create a native (C++) backing store for the HTML element inside HTMLElement’s constructor (or an equivalent code that runs as a part of call to `super()` from the direct subclass of HTMLElement) since the newly constructed (this) element is never accessed until the highest superclass' `super()` (which is HTMLElement in this case) had been called.

- R. Niwa

Received on Saturday, 18 July 2015 02:34:43 UTC