ES6 doesn't need opt-in

John J Barton johnjbarton at johnjbarton.com
Sun Jan 1 10:45:03 PST 2012


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

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

Sure, but in that case the test
  typeof define === 'function'
tests the value that the variable 'define' references.  It does not test if
"define' has been 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.)
>
>
> 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
>

Ok now I understand David's comment about ReferenceError. If you try to use
instanceof for these kinds of tests, you have to prefix every use with a
test it ensure that the variable exists.

I prefer the former expression, because it is clearer.



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

Well myvar is declared so how can exists be false?

You use of "declared" is what has thrown me off.

You really mean is "the value of the reference has been set to |undefined|
or |null|, either explicitly or by default" right?

(I think this is why so many JS devs avoid |null|. If you don't use |null|
in your code, then typeof compared to |undefined| works great).


>
>     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/7345a0ff/attachment.html>


More information about the es-discuss mailing list