Cross-global instanceof

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Oct 31 11:01:48 PDT 2013


On Oct 31, 2013, at 9:43 AM, Brandon Benvie wrote:

> On 10/31/2013 8:50 AM, David Bruant wrote:
>> I'm not sure it's worth making it work for jQuery. This is trying to make a good use of same-origin multi-global which shouldn't exist in the first place. Keeping same-origin access as it is and encouraging people to add @sandbox even on same-origin iframes seems like a better idea.
>> 
>> Should the addition be a nicer Object.prototype.toString.call?
> 
> You're right, it's not instanceof. But I definitely think the argument can be made that there is a need for checking if something is of a certain class. I'm finding myself wanting things like `isGenerator` and `isGeneratorFunction`, for example. There's countless examples of people re-purposing Object.prototype.toString to serve this functionality ad hoc.


What are the use cases for things like 'isGenerator'. When and why would you need to know that an object  upon which you are going to invoke the Iterator interface was/wasn't implemented by a generator.  If the reason is to know whether or not you can use the throw method (the only real difference between the generator object interface and the Iterator interface) you can test:
    'throw' in possibleGenerator

In general this sort of isFoo or obj.constructor === Foo class test is a bad smell in dynamic class based languages and tends to be a reflection of static nominal type thinking (which is fine in its own context, but JS is has a dynamic class model).

Allen


More information about the es-discuss mailing list