new instantiation design alternatives

Brendan Eich brendan at mozilla.org
Mon Sep 15 15:48:38 PDT 2014


Rick Waldron wrote:
> On Mon, Sep 15, 2014 at 2:57 PM, Brendan Eich <brendan at mozilla.org 
> <mailto:brendan at mozilla.org>> wrote:
>
>     Rick Waldron wrote:
>
>         The first is also objectionable because it breaks existing
>         implicit return semantics.
>
>
>     Say what? Constructors can return a different object from `this`,
>     that's just JS.
>
>
> Yikes, I should've been more specific. Generally, it's considered an 
> anti-pattern to write constructors that explicitly return an object to 
> override `this`(for all the reasons you'd expect)—that's not to say 
> that it isn't done or doesn't exist (and certainly I didn't mean to 
> imply that it wasn't possible).

Oh, your "is also objectionable" in the context of a design discussion 
sounded like a "shouldn't be expressible", not "some frown on that" :-|.

(dherman among others is a fan of return-override; anyway, it's here to 
stay, including in class constructors.)

> Design that might rely on that pattern would conflict with widely 
> accepted best practices.

Allen's `newO` example was showing a longer form alternative to `this = 
new super(...)`, as a kind of worst to worse comparison where we all 
seek better. It was not talking about what's good or bad style. The 
`newO` example's use of return-override ain't great if you frown on that 
practice, but it is the least of our worries.

It also composes in any event. How a constructor returns the new object 
doesn't break subclassing or anything else, I hope! Someone holler if 
I'm wrong.

/be


More information about the es-discuss mailing list