ES6 doesn't need opt-in

John J Barton johnjbarton at johnjbarton.com
Sun Jan 1 09:05:40 PST 2012


On Sun, Jan 1, 2012 at 6:32 AM, Dean Landolt <dean at deanlandolt.com> wrote:

>
>
> On Sun, Jan 1, 2012 at 12:12 AM, John J Barton <
> johnjbarton at johnjbarton.com> wrote:
>
>>
>>
>> On Sat, Dec 31, 2011 at 7:53 PM, Axel Rauschmayer <axel at rauschma.de>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.
>>
>
>
> Tell that to people writing feature detection code :)
>

Sorry I don't understand what you mean here. I thought feature detection
relied on testing for the existence of certain object properties and
functions.


>
>
>
>> 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) {}
>>
>
>
> And if name is 0? Or ""? Whoops. Javascript's coercion is pretty winful
> here (in spite of what jslint might say): `if (name == null)` is more
> targeted, just catching the name === null and name === void 0 cases.
>
>

I was challenging Axel's characterization of how JS devs use typeof, since
it leads him to conclude that we need to add a bunch of new language
features. In code I see, devs don't use typeof for checking a value is null
or undefined, so don't base new language features on this use case.

I agree that the common practice of testing if(!name) has pitfalls. But
doesn't the common practice suggest that more elaborate features will not
help? We need something as simple as if (!name).

jjb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120101/2332b74f/attachment.html>


More information about the es-discuss mailing list