Proposal: allow primitives to be explicitly returned from constructors

Isiah Meadows isiahmeadows at gmail.com
Sat Apr 21 00:15:49 UTC 2018


My rationale was detailed in a response to another person eariler:

> But my idea was just to bring `new` and normal calls a little closer
to one another. Eventually, I'd like to see if primitive wrapper types
could disappear, and this is one of the areas where I'm trying to see
if it's feasible to do in JS. (Wrapper types are actually a
complicating factor in JS optimization and the object model in
general.)

>From a reply that (I presume accidentally) missed the list, It doesn't
appear to be web-compatible, so this entire discussion would qualify
as moot:

https://www.chromestatus.com/metrics/feature/timeline/popularity/2054

-----

Isiah Meadows
me at isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Fri, Apr 20, 2018 at 7:34 PM, Michael Theriot
<michael.lee.theriot at gmail.com> wrote:
> I don't understand the use case. If anything I would like it if returning an
> object that fails `new Class instanceof Class` to also fail, not permit even
> more oddities.
>
> On Thu, Apr 19, 2018 at 6:49 PM, Isiah Meadows <isiahmeadows at gmail.com>
> wrote:
>>
>> Here's my proposal:
>>
>> In constructors, currently, non-objects are replaced with `this`.
>> Here's what I think it should be replaced with:
>>
>> 1. When calling the constructor, if `undefined` is returned and
>> `new.target` is not `undefined`, return `this` instead. This is for
>> compatibility and ease of implementation.
>> 1. When calling the constructor, if anything else is returned, return
>> that value in raw form.
>>
>> I know this is very *likely* very breaking, but I wonder if it would
>> be possible.
>>
>> In case you're curious what this would change in the spec, it would
>> change [section 9.2.2][1], step 13.a-13.c to this:
>>
>> a. If result.[[Value]] is not `undefined`, return
>> NormalCompletion(`result.[[Value]]`).
>> b. If kind is `"base"`, return NormalCompletion(thisArgument).
>>
>> [1]:
>> https://tc39.github.io/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
>>
>> -----
>>
>> Isiah Meadows
>> me at isiahmeadows.com
>>
>> Looking for web consulting? Or a new website?
>> Send me an email and we can get started.
>> www.isiahmeadows.com
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>


More information about the es-discuss mailing list