Strawman proposal: new `is` operator

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Aug 25 02:03:47 PDT 2014


I know it's just a sketch but that `Object.prototype.is` does not play well
with `Object.create` where no constructor is necessary to create `is`
relations.

Regards


On Sun, Aug 24, 2014 at 10:15 PM, C. Scott Ananian <ecmascript at cscott.net>
wrote:

> On Sun, Aug 24, 2014 at 4:04 PM, Isiah Meadows <impinball at gmail.com>
> wrote:
> > There is no way currently to reliably verify types in JS. Type
> annotations
> > cannot solve this completely, especially when you have an overloaded
> > function. A way to reliably verify types would make this far easier. So
> far,
> > here are the current options with their respective caveats:
> >
> > 1. `instanceof` - This returns true for the class, but also for
> subclasses.
> > This isn't always desired.
>
> Your proposal fixes this, but does not address the other issues you raise:
>
> > 2. `this.constructor === Foo` - The constructor property could easily be
> > overwritten, rendering this relatively useless.
>
> `Object.getPrototypeOf(this) === Foo.prototype` is the test which
> `instanceof` uses.
> Does not require new syntax.
>
> > 3. `typeof` - This only is useful for literals, with the exception of
> > `null`.
>
> Your proposed `is` operator is not useful for literals.
>
> > 4. `Object.prototype.toString.call()` - This returns the correct native
> > object type for native objects, but returns `[object Object]` for all
> > non-native objects, rendering it effectively useless. This includes
> object
> > instances created from `class Foo {}`, etc.
>
> In general, you can always define:
> ```
> Object.prototype.is = function(constructor) {
>   return Object.getPrototypeOf(this) === constructor.prototype;
> };
> ```
> rather than try to use toString.  Unless you want it to work on literals...
>   --scott
> _______________________________________________
> 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/20140825/f5b8c060/attachment.html>


More information about the es-discuss mailing list