ES6 doesn't need opt-in

Axel Rauschmayer axel at rauschma.de
Sat Dec 31 19:53:24 PST 2011


> 1. We want sane isObject and isNull predicates, ideally using typeof. Lack of them continues to bite people, as the web contains code that wrongly assumes typeof x == "object" => x.foo won't throw on null x.

What are the use cases for typeof? Offhand, I see five and for most of them, other solutions seem to be better.

1. Checking whether a variable has been declared.
    Problematic: verbose and conflated with checking for a declared variable having the value `undefined`.
    Better: a dedicated operator or predicate for performing this check.

2. Checking that a value is neither null nor undefined.
    Problematic: can’t be done via only typeof currently.
    Better: a predicate for performing this check. This use case will become less important with default parameter values.

3. Distinguishing between objects and primitives.
    Problematic: Made more difficult by typeof null === "object" and typeof function () {} === "function".
    Better: predicates such as isObject() and isPrimitive()

4. Determining the type of a primitive value.
    Better: typeof is OK here, but changing it so that typeof null === "null" would help.

5. Determining the type of a value (primitive or otherwise).
    Better: I would want a function, e.g. getTypeName() that works like (null-enabled) typeof for primitives and returns the value of the [[Class]] property for objects.

Everything except #1 can be easily implemented as functions (and be brought to ES5 via shims). A function such the #5 getTypeName() could take care of use case #4, as well.

Ideas for getTypeName(): http://www.2ality.com/2011/11/improving-typeof.html

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


More information about the es-discuss mailing list