Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17966 > unrolled thread

typeof and jslint

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2013-01-06 10:17 +1100
Last post2013-01-06 01:21 +0100
Articles 8 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  typeof and jslint Andrew Poulos <ap_prog@hotmail.com> - 2013-01-06 10:17 +1100
    Re: typeof and jslint Luc Yen <luc@goal.tw> - 2013-01-05 15:40 -0800
      Re: typeof and jslint Andrew Poulos <ap_prog@hotmail.com> - 2013-01-06 11:04 +1100
        Re: typeof and jslint Luc Yen <luc@goal.tw> - 2013-01-05 16:33 -0800
    Re: typeof and jslint David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 16:16 -0800
      Re: typeof and jslint Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-01-11 00:53 +0100
    Re: typeof and jslint Stefan Weiss <krewecherl@gmail.com> - 2013-01-06 01:21 +0100
    Re: typeof and jslint Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-06 01:21 +0100

#17966 — typeof and jslint

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-01-06 10:17 +1100
Subjecttypeof and jslint
Message-ID<Ba2dnXXmp_MKKXXNnZ2dnUVZ_sydnZ2d@westnet.com.au>
If I test the following with jslint
   if (typeof num === 'undefined') { return false; }

I get the error
   Unexpected 'typeof'. Use '===' to compare directly with undefined.

I'm not sure what its telling me. Is it saying that I should instead use
   if (num === undefined) { return false; }
?

Why is tyepof unexpected?

Andrew Poulos

[toc] | [next] | [standalone]


#17967

FromLuc Yen <luc@goal.tw>
Date2013-01-05 15:40 -0800
Message-ID<90b3cbc7-939c-44a3-8412-bc1f3630167f@googlegroups.com>
In reply to#17966
Andrew Poulos於 2013年1月6日星期日UTC+8上午7時17分29秒寫道:
> If I test the following with jslint
>    if (typeof num === 'undefined') { return false; }
> I get the error
>    Unexpected 'typeof'. Use '===' to compare directly with undefined.
[...]
> Why is tyepof unexpected?

$ jslint --browser filename.js

[toc] | [prev] | [next] | [standalone]


#17969

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-01-06 11:04 +1100
Message-ID<O-adnQikndziInXNnZ2dnUVZ_vOdnZ2d@westnet.com.au>
In reply to#17967
On 6/01/2013 10:40 AM, Luc Yen wrote:
> Andrew Poulos於 2013年1月6日星期日UTC+8上午7時17分29秒寫道:
>> If I test the following with jslint
>>     if (typeof num === 'undefined') { return false; }
>> I get the error
>>     Unexpected 'typeof'. Use '===' to compare directly with undefined.
> [...]
>> Why is tyepof unexpected?
>
> $ jslint --browser filename.js

Is that an answer to my questions? If it is, I don't understand it.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#17974

FromLuc Yen <luc@goal.tw>
Date2013-01-05 16:33 -0800
Message-ID<73eb3b7a-9d14-4d89-9ffb-8e504d75fb83@googlegroups.com>
In reply to#17969
Andrew Poulos於 2013年1月6日星期日UTC+8上午8時04分01秒寫道:
> On 6/01/2013 10:40 AM, Luc Yen wrote:
> 
> > Andrew Poulos於 2013年1月6日星期日UTC+8上午7時17分29秒寫道:
> 
> >> If I test the following with jslint
> 
> >>     if (typeof num === 'undefined') { return false; }
> 
> >> I get the error
> 
> >>     Unexpected 'typeof'. Use '===' to compare directly with undefined.
> 
> > [...]
> 
> >> Why is tyepof unexpected?
> 
> >
> 
> > $ jslint --browser filename.js
> 
> 
> 
> Is that an answer to my questions? If it is, I don't understand it.
> 
> 
> 
> Andrew Poulos
jslint enforces it's coding style. The following snippet is ok.
The typeof line is identical to the original code.
-----
var num;
function test() {
    "use strict";
    if (typeof num === 'undefined') {
        return false;
    }
}

[toc] | [prev] | [next] | [standalone]


#17970

FromDavid Mark <dmark.cinsoft@gmail.com>
Date2013-01-05 16:16 -0800
Message-ID<6322550a-f3ab-4e2a-b95e-cc8a52569e8f@u19g2000yqj.googlegroups.com>
In reply to#17966
On Jan 5, 6:17 pm, Andrew Poulos <ap_p...@hotmail.com> wrote:
> If I test the following with jslint
>    if (typeof num === 'undefined') { return false; }
>
> I get the error
>    Unexpected 'typeof'. Use '===' to compare directly with undefined.
>
> I'm not sure what its telling me. Is it saying that I should instead use
>    if (num === undefined) { return false; }
> ?
>
> Why is tyepof unexpected?
>

Because Crockford thinks you should compare to the undefined value.
That's all.

Of course, that's not typically the way it is done for host object
detection. Theoretically using the strict comparison would avoid at
least some of the known pitfalls, but typeof has always served me well
in discovering host objects and their methods. I agree that it makes
sense to avoid typeof for native comparisons. Many years ago typeof
was used to avoid /undefined/ out of compatibility concerns, but that
issue is long dead.

[toc] | [prev] | [next] | [standalone]


#18060

FromEric Bednarz <bednarz@fahr-zur-hoelle.org>
Date2013-01-11 00:53 +0100
Message-ID<m2txqomw78.fsf@nntp.bednarz.nl>
In reply to#17970
David Mark <dmark.cinsoft@gmail.com> writes:

> [...] Many years ago typeof
> was used to avoid /undefined/ out of compatibility concerns, but that
> issue is long dead.

Between that and ES5, `undefined' was writable.

[toc] | [prev] | [next] | [standalone]


#17972

FromStefan Weiss <krewecherl@gmail.com>
Date2013-01-06 01:21 +0100
Message-ID<kcag1j$ld0$1@news.albasani.net>
In reply to#17966
On 2013-01-06 00:17, Andrew Poulos wrote:
> If I test the following with jslint
>    if (typeof num === 'undefined') { return false; }
> 
> I get the error
>    Unexpected 'typeof'. Use '===' to compare directly with undefined.

That's just plain wrong. "num === undefined" may do the same thing, but
only if num has been declared or can be otherwise resolved from the
current scope. If you can't be sure that num exists, typeof is mandatory.

This is bad advice, and yet another reason not to use JsLint. You could
try to report this on the jslint_com mailing list, and hope that DC
agrees with you. Or you could use another linting tool like JsHint.


- stefan


PS: In addition to the above, the 'undefined' identifer is not
guaranteed to evaluate to the undefined type, but that's more of a
theoretical concern. Your original code would be safe from that.

[toc] | [prev] | [next] | [standalone]


#17973

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-01-06 01:21 +0100
Message-ID<2177465.0Ca87JebjB@PointedEars.de>
In reply to#17966
Andrew Poulos wrote:

> If I test the following with jslint
>    if (typeof num === 'undefined') { return false; }
> 
> I get the error
>    Unexpected 'typeof'. Use '===' to compare directly with undefined.
> 
> I'm not sure what its telling me. Is it saying that I should instead use
>    if (num === undefined) { return false; }
> ?

Probably, yes.
 
> Why is tyepof unexpected?

Because Douglas Crockford has some rather weird and purely subjective 
notions how these languages – that he misleads others to think of as a 
single one – should be written, and those notions are expressed and
enforced by jslint.


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web