On Sat, Dec 31, 2011 at 7:53 PM, Axel Rauschmayer <[email protected]> wrote:
> 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.
>
In my experience the major use of typeof is to implement dynamic function
overloading. That is, a function that is flexible in the arguments it
takes, such that one logical name maps to multiple use cases.
>
> 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.
>
Sorry, I don't think anyone checks if a is variable declared. You just look
at the source code.
>
> 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.
>
if(!name) {}
>
> 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()
>
Mostly we are checking string, array, function or object. Only Java devs
pass/return |null| so that case is not that big of a deal. Or another way
to look at it: if you use null, it's your own little signal, so enjoy.
Array is the famously difficult case.
>
> 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.
>
This would be great!
>
> 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
> [email protected]
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss