ES6 doesn't need opt-in

John J Barton johnjbarton at johnjbarton.com
Sun Jan 1 09:21:57 PST 2012


On Sun, Jan 1, 2012 at 9:09 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

> > Maybe I am confused. Here is what I was imagining:
> >   var w = 1;  // w is declared and defined. typeof gives 'number'
> >   var x;  // x is declared, but is undefined. typeof gives 'undefined'
> >   y = 4; // y is not declared, is a global.    typeof gives 'number'
> >            // z is not declared                     typeof gives
> 'undefined.
> >
> > So how can I check to see that y and z are not declared?
> >
> > In:
> >   var b;
> >   if (typeof x !== 'undefined') b = x
> >
> > I don't get a ReferenceError. What I am I missing?
>
> Not a frequent use case (again, even less frequent with modules in ES6),
> but it exists: Boilerplate to Node.js-enable an asynchronous module
> definition.
>
>    ({ 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"? 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.

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.)

jjb



> Alternative: wrap a function around define(), make define a parameter,
> perform the (same!) check when immediately-invoking the function.
>
> --
> 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/681bdfd6/attachment.html>


More information about the es-discuss mailing list