ES6 doesn't need opt-in

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


> 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
    });
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





More information about the es-discuss mailing list