ES6 doesn't need opt-in

Axel Rauschmayer axel at rauschma.de
Mon Jan 2 19:21:34 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
>>    });

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

I’m not completely sure I understand what you mean.

typeof define === "function"
- Node.js: variable "define" has not been declared. Because of that, typeof define is "undefined" and the expression is false.
- Browser + RequireJS: There is a global variable "define" whose value is a function. Hence typeof define is "function" and the expression is true.

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

Right. Maybe the operator should have a different name. "isDefined"? "has a value" expressed as an operator name?

     isDefined x

would be syntactic sugar for

     typeof x !== "undefined" && x !== null

The expression would not throw an exception if x hasn’t been declared.

The two most important use cases for typeof probably are:

- isDefined
- Helping to implement getTypeName() whose results are
    - "null", "undefined", "boolean", "number", "string" for primitive values
    - [[Class]] for objects ("String", "Date", etc.)

-- 
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/20120103/d22a1fbe/attachment.html>


More information about the es-discuss mailing list