ES6 doesn't need opt-in

Axel Rauschmayer axel at rauschma.de
Sun Jan 1 09:45:42 PST 2012


>    ({ define: typeof define === "function"
>        ? define  // browser
>        : function(F) { F(require,exports,module) } }).  // Node.js
>    define(function (require, exports, module) {
>        // Node.js module code goes here
>    });
> 
> Ok, thanks for the example. Here the test 
>   typeof define === 'function'
> is equivalent to 
>   typeof window.define === 'function'
> Is this testing whether 'define' is "declared"?

Yes.

> I thought that 'window' is built-in so window.foo can either have a value or be 'undefined', but window.foo can not be declared.

Note: `window` does not exist on Node.js (and possibly other non-browser environments).

> Anyway, if this example illustrates your use case, then it is the important case in my opinion (dynamic function overloading, supporting multiple callers). (And I don't see how any dedicated operator can help.)


If there was an operator `exists`, the following two expressions would be equivalent. I prefer the latter expression, because it is more explicit.

     typeof define === "function"
     exists define && define instanceof Function

The exists operator would work as follows (slightly edited from a previous email):

    console.log(exists globalUnknown); // false

    // undefined and null in several variations

    var myvar;
    console.log(exists myvar); // false

    console.log(exists undefined); // false
    console.log(exists null); // false

    var obj = {};
    console.log(exists obj.prop); // false

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120101/3a6038ca/attachment-0001.html>


More information about the es-discuss mailing list