This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 24018 - [Custom]: No way to pass parameters to constructor
Summary: [Custom]: No way to pass parameters to constructor
Status: RESOLVED FIXED
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: HISTORICAL - Component Model (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 normal
Target Milestone: ---
Assignee: Dimitri Glazkov
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 14968
  Show dependency treegraph
 
Reported: 2013-12-06 15:40 UTC by Erik Arvidsson
Modified: 2014-05-12 22:14 UTC (History)
1 user (show)

See Also:


Attachments

Description Erik Arvidsson 2013-12-06 15:40:58 UTC
DOM has a bunch of element constructors that take parameters. `new Image(src)` for example. Custom elements fail in this regard. The following should just work.

```js
class MyElement extends HTMLElement {
  constructor(text) {
    this.textContent = text;
  }
}
MyElement = document.register('my-element', MyElement);
new MyElement('Hi');
```
Comment 1 Dominic Cooney 2013-12-08 00:21:58 UTC
I think this should be addressed in Level 2 of the Custom Elements spec, or in a related spec. Chrome is not in a position to implement this now.

Does ES6 make it possible to detect whether a function is a "class" or has a constructor?

I believe this is a simple matter of specifying that, for such functions, the generated constructor passes arguments to the argument's constructor.

It would be useful for someone with deep familiarity with ES6 to explain in what other ways the generated constructor should be configured.
Comment 2 Erik Arvidsson 2013-12-08 23:15:26 UTC
> Does ES6 make it possible to detect whether a function is a "class" or has a constructor?

There is no difference between a class and a function. The only way to tell the difference is in what Function.prototype.toString would return. http://people.mozilla.org/~jorendorff/es6-draft.html#sec-function.prototype.tostring

The constructor is always the function representing the class.

```js
class C {}
assert(C === C.prototype.constructor);

class C2 {
  constructor() {}
}
assert(C2 === C2.prototype.constructor);
```

Just like `Date === Date.prototype.contstructor` and `HTMLElement === HTMLElement.prototype.constructor`.

When writing "classes" using ES3/ES5 libraries such as Closure, Backbone, Ember, Ext.js, YUI etc they will set up the prototype.constructor so that this equivalence also holds.
Comment 3 Dominic Cooney 2013-12-09 00:44:27 UTC
(In reply to Erik Arvidsson from comment #2)
> > Does ES6 make it possible to detect whether a function is a "class" or has a constructor?
> 
> There is no difference between a class and a function.

OK. So it makes sense to pass the parameters through in the case where the second argument to register is a function.