Should I be able to apply `new`?
Graydon Hoare
graydon at mozilla.com
Fri Apr 6 13:11:09 PDT 2007
P T Withington wrote:
> I thought I could memoize by simply returning a value from my
> constructor. Is that no longer true?
Ah, yes. A subtlety in the word "constructor": you can return any novel
value you like from a constructor *function*, as this is legal ES3. But
in ES4 we also have classes with constructors, and you cannot return
novel values from a class constructor.
Class constructors are called as part of a specific sequence of nested
base/derived actions, including instance initializers declared outside
the ctor and instance settings declared between the parameter list and
the ctor body. That sequence of actions assumes a certain continuity in
the constructee: switching the value you're part-way through
constructing in the middle of the sequence makes it very hard to
formulate safety conditions that would preserve type judgments the
verifier might wish to make, so we've prohibited it.
(In general any valid ES3 code, save a very few ill-specified or
horrendously dangerous and unused cases, should be valid ES4 code. We
know we can't break the web :)
-graydon
More information about the Es4-discuss
mailing list