constructor, super, and data members issue

Logan Smyth loganfsmyth at gmail.com
Fri Aug 24 23:34:55 UTC 2018


Generally if something is required during construction, it would be best to
pass it down as part of the constructor options. For example, you could do
```
class Base {
  constructor({ idAttribute = "id"}) {
    this.idAttribute = idAttribute;
  }
}

class Derived extends Base {
  constructor() {
    super({
      idAttribute: '_id'
    });
  }
}
```

I don't think class fields would be a good way to conceptually do this kind
of thing.


On Fri, Aug 24, 2018 at 2:56 PM, Aaron Gray <aaronngray.lists at gmail.com>
wrote:

> Yeah it does look like its badly "broken by design".
>
> On Fri, 24 Aug 2018 at 22:13, Jordan Harband <ljharb at gmail.com> wrote:
>
>> I'm afraid that still wouldn't solve the problem; the superclass's code
>> is all 100% completed before the subclass has `this` available.
>>
>> On Fri, Aug 24, 2018 at 1:52 PM, Ranando King <kingmph at gmail.com> wrote:
>>
>>> Aaron, congratulations! You just tripped over a new reason for me to
>>> revive issue #123. The only way to get that to work is to have the default
>>> values on the prototype. The problem comes because `this` doesn't even have
>>> a value until the last call to `super()` returns. If a `class` doesn't have
>>> a base `class` it essentially has Object as a base `class` and `super` is
>>> implicitly called. So unless the default public field values are on the
>>> prototype, there's literally no way to have them initialized before the
>>> base classes are initialized.
>>>
>>> On Fri, Aug 24, 2018 at 3:15 PM Aaron Gray <aaronngray.lists at gmail.com>
>>> wrote:
>>>
>>>> I am having an issue with order semantics regarding https://github.com/
>>>> tc39/proposal-class-fields with derived classes defining or overriding
>>>> data member values that are used in the base class constructor for
>>>> initialization of properties of the class.
>>>>
>>>> This means the Super Class / Base Class'es constructor does not yet
>>>> have access to the default field values of the derived class it is
>>>> initiating.
>>>>
>>>>     class Base {
>>>>         constructor() {
>>>>              ....
>>>>              .. idAttribute ..
>>>>              ....
>>>>         }
>>>>         idAttribute = 'id';
>>>>     }
>>>>    class Derived extends Base {
>>>>         constructor() {
>>>>              super();
>>>>              ....
>>>>         }
>>>>         idAttribute = '_id';
>>>>    }
>>>>
>>>> All would mean having a separate initialize() function, but even this
>>>> solution is flawed when there is a third level in the hierarchy. And as
>>>> super() is required it means there seems to be no way round this issue. The
>>>> only way I can see is some form of override keyword ?
>>>>
>>>>
>>>> Has anyone got any solutions to this issue or work arounds ?
>>>> --
>>>> Aaron Gray
>>>>
>>>> Independent Open Source Software Engineer, Computer Language
>>>> Researcher, Information Theorist, and amateur computer scientist.
>>>> _______________________________________________
>>>> es-discuss mailing list
>>>> es-discuss at mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
> --
> Aaron Gray
>
> Independent Open Source Software Engineer, Computer Language Researcher,
> Information Theorist, and amateur computer scientist.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180824/091c3627/attachment-0001.html>


More information about the es-discuss mailing list