Proposal: allow primitives to be explicitly returned from constructors

Isiah Meadows isiahmeadows at gmail.com
Fri Apr 20 01:59:51 UTC 2018


You already can't assume the result is `typeof new function () {
return value } === "object"` for every `value` - you can return
functions out of them. Also, you could migrate that to avoid creating
any temporary objects at all - here's what I usually do:

```
function isObject(value) {
    return value != null && (
        typeof value === "object" ||
        typeof value === "function"
    )
}
```

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.)

But of course, this could all break the web, and if you have no choice
in the matter (say, legacy code base with *way* too many instances of
it), it would be too breaking to even be worth doing this.

-----

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 Thu, Apr 19, 2018 at 7:22 PM, Oriol _ <oriol-bugzilla at hotmail.com> wrote:
> This would break one of my most preferred ways to check whether a value is
> an object:
>
> ```js
> function isObject(value) {
>   return value === new function(){ return value };
> }
> ```
>
> Unlike `Object(value) === value`, the above is just syntactic, it does not
> require `Object` to be the built-in one.
>
> And I don't really see the point, constructors are supposed to construct
> objects. If you want primitives, you can always use function calls.
>
> -- Oriol
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list