Strawman proposal: new `is` operator

Isiah Meadows impinball at gmail.com
Mon Aug 25 15:07:04 PDT 2014


Cc the list...
On Aug 25, 2014 6:06 PM, "Isiah Meadows" <impinball at gmail.com> wrote:

> There really shouldn't be any sort of object construction needed to check
> types like this. `isa` may be better, anyways, but I still find that
> requirement to build and destroy an object to check somewhat
> counterproductive.
>
> Andrea gets what I'm talking about. Also, another (possibly separate)
> proposal would be to make cases like `"foo" instanceof String" === true`
> instead of their current behavior, throwing a TypeError. (I believe...I'm
> on a phone, not a PC where I can test. Correct me if I'm wrong.)
> On Aug 25, 2014 5:03 AM, "Andrea Giammarchi" <andrea.giammarchi at gmail.com>
> wrote:
>
>> 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/4860a253/attachment.html>


More information about the es-discuss mailing list