Re: Why can't we just use constructor instead of createdCallback?

On Fri, Dec 6, 2013 at 2:33 AM, Ryosuke Niwa <rniwa@apple.com> wrote:

> It appears to me that we should definitely have a good answer for this
> question before the specification reaches CR
> given that the definition of ES6 classes is pretty stable at this point.


ES6 classes do not introduce any new semantics over ES5. However I do agree
that we need to think about user ergonomics here. If we cannot use ES6
class syntax to do custom elements we have failed. What we have now is OK
but not perfect.

Here is how you can use class syntax today

```js
class MyElement extends HTMLElement {
  createdCallback() {
    console.log("I'm not a real boy");
  }
}
MyElement = document.create('my-element, MyElement);

// or

var MyElement = document.create('my-element, class extends HTMLElement {
  createdCallback() {
    console.log("I'm not a real boy");
  }
});
```

This works fine in Blink today: http://goo.gl/HZLpqG

The relevant addition to ES6 is how @@create participates in object
creation.

I've previously voiced concern that custom elements do not use @@create and
the spec draft was changed in a way where it would allow using it in the
future. There was strong concern from Mozilla that they did not want custom
elements to depend on this ES6 feature due to them needing
document.register ASAP.

The way this would work without custom elements is:

```js
class MyElement extends HTMLElement {
  constructor(name) {
    console.log(`I'm a real boy and my name is ${name}`);
  }
  [Symbol.create]() {
    return document.createElement('my-element');
  }
}
new MyElement('Pinocchio');
```

The things that fail here are:

1. The parser does not know that it needs to use MyElement.@@create to
create the JS objects when it sees a <my-element>.
2. No callbacks for enteredView, leftView and attributeChanged.
3. It depend on the magic of document.createElement which is circular. A
better way would be to do `super('my-element')` or something like that.

I wish we could resolve these remaining issues.

-- 
erik

Received on Friday, 6 December 2013 15:38:38 UTC