typeof null

Axel Rauschmayer axel at rauschma.de
Wed May 9 17:23:59 PDT 2012

>> - Introduce a binary predicate, e.g. likeInstanceOf that correctly handles cases where the lhs and rhs come from different contexts/frames.
> Again, how? The name of the constructor in one frame may not be relevant in the other. The constructor may not be a built-in. Are you thinking in nominal type terms again? :-/

Strawman for a polyfill:

    function hasClass(value, type) {
        // Possibly: throw if `type` is not a built-in or does not have a property `name`
        return Object.prototype.toString.call(value) === "[object "+type.name+"]";


    $ hasClass([], Array)
    $ hasClass([], RegExp)

Alternative: work with string values.

    function hasClass(value, typeName) {
        if (typeof typeName !== "string") {
            throw new TypeError("...");
        return Object.prototype.toString.call(value) === "[object "+typeName+"]";


    $ hasClass([], "Array")
    $ hasClass([], "RegExp")

Admittedly, this solution has its own problems. But, IMO, it is an improvement to having one predicate per builtin. Plus, it would work with non-builtins, too. A function getClassName() (as suggested by Jorge) would work just as well.

    $ getClassName(arrayFromAnotherWindow)
    $ getClassName(regexFromAnotherWindow)

Do modules change anything? Are module instances shared between windows? Will there be a unique way of identifying types? Are there any other ideas for making the cross-window situation simpler?

Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

More information about the es-discuss mailing list