Why are object initializer methods not usable as constructors?

/#!/JoePea joe at trusktr.io
Wed Jul 27 18:44:45 UTC 2016

What's the good reason that object-initializer methods can't be
constructors though? I mean, I get that "that's what spec says", but what's
the actual good reason?

The problem here would be solved with a dynamic super.

I feel that ES6 diverges from ES5 in backwards-incompatible ways. Some
people say ES6 classes are "syntax sugar" over the ES5 constructor pattern
classes, but, *not really*, since they aren't compatible in the same
places. ES6 classes  and object-initializer methods are not
backwards-incompatible in a bad way because they cannot be treated the same
as we are used to in ES5 (f.e. copying methods with underscore's
`_.extend()` doesn't work because of static HomeObjects), and that is
backwards-incompatible with previous concepts, ideas, and methodologies
that we love JavaScript for.

Some old code might do something like

_.extend(SomeClass.prototype, OtherClass.prototype)

where `SomeClass` and `OtherClass` are ES5 constructor-pattern classes. If
the classes are updated to be ES6 classes, then that code may fail because
of the static `HomeObject` properties, and that is backwards-incompatible
with the paradigms and patterns of ES5.

**A dynamic super would remedy this problem.**

For now, I am contemplating using a custom and dynamic
`Object.prototype.super` getter in all apps where my code runs so that my
ES6 classes remain as flexible as in ES5. The overhead associated with a
custom `Object.prototype.super` would only be due to the fact that it is
not possible for us to hook into the native property lookup algorithm,
otherwise the overhead of finding a HomeObject would be completely
negligible because the native property lookup algorithm already finds the
HomeObject where a property or method is located and therefore it would
cost next to nothing to use the found HomeObject on a method call by
passing HomeObject as an argument in the native side of the JS engine.


On Wed, Jul 27, 2016 at 7:58 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>

> The most common case is the “methods” are designed to be used as
> constructors.  This has always be recognized for built-in methods in the
> ECMAScript specification  which says: "Built-in function objects that are
> not identified [in this specification] as constructors do not implement the
> [[Construct]] internal method unless otherwise specified in the description
> of a particular function.”.   When “concise methods” where add as part of
> Object Initializers and Class Definitinos this default was applied to them.
> You can still define constructible properties for Object Initializers,
> just not by using concise method syntax:
> ```js
> let classes = {
>   Cat: function() {},
>   Dog: function() {},
>   Bird: class {}
> };
> let [c, d, b] = [new classes.Cat, new classes.Dog, new classes.Bird];
> ```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160727/b6819a10/attachment-0001.html>

More information about the es-discuss mailing list