- From: Tobie Langel via GitHub <sysbot+gh@w3.org>
- Date: Mon, 18 May 2015 09:52:39 +0000
- To: public-device-apis@w3.org
tobie has just created a new issue for https://github.com/w3c/sensors:
== No way to express an abstract constructor in WebIDL ==
Emailed @heycam on this topic.
Current proposal requires a constructor on the abstract super class to
handle common instantiation logic, but that constructor shouldn't be
instantiatable.
Here's how this would be roughly built in ES5:
```js
function AbstractSensor(options) {
if (Object.getPrototypeOf(this) === AbstractSensor.prototype) {
throw new TypeError("Illegal constructor");
}
this.frequency = options.frequency || 1;
}
function TemperatureSensor(options) {
AbstractSensor.call(this, {
frequency: options.frequency
});
this.unit = options.unit || "C";
}
inherit(TemperatureSensor, AbstractSensor);
function inherit(sub, sup) {
sub.prototype = Object.create(sup.prototype);
Object.defineProperty(sub.prototype, "constructor", {
configurable: true,
enumerable: false,
writable: true,
value: sub
});
}
new TemperatureSensor({ unit: "F", frequency: 60 });
// returns TemperatureSensor instance
new AbstractSensor({ frequency: 60 });
// throws TypeError
```
So something like an `AbstractConstructor` [extended attribute][1] in
WebIDL
would work. It would be behave just like the [normal `Constructor`
extended
attribute][2] but would throw when not "subclassed."
[1]: http://heycam.github.io/webidl/#idl-extended-attributes
[2]: http://heycam.github.io/webidl/#Constructor
See https://github.com/w3c/sensors/issues/19
Received on Monday, 18 May 2015 09:52:40 UTC