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


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

jslint and typeof

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2013-05-20 19:59 +1000
Last post2013-05-20 23:17 +1000
Articles 8 — 5 participants

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


Contents

  jslint and typeof Andrew Poulos <ap_prog@hotmail.com> - 2013-05-20 19:59 +1000
    Re: jslint and typeof "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-05-20 13:40 +0300
      Re: jslint and typeof Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-05-20 13:34 +0200
        Re: jslint and typeof Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-20 14:23 +0200
          Re: jslint and typeof Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-05-26 17:40 +0200
    Re: jslint and typeof Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-20 13:48 +0200
    Re: jslint and typeof Stefan Weiss <krewecherl@gmail.com> - 2013-05-20 14:44 +0200
      Re: jslint and typeof Andrew Poulos <ap_prog@hotmail.com> - 2013-05-20 23:17 +1000

#19837 — jslint and typeof

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-05-20 19:59 +1000
Subjectjslint and typeof
Message-ID<kr2dnRlKLtGEaQTMnZ2dnUVZ_g6dnZ2d@westnet.com.au>
A fragment of some code I've been given has this

if (typeof options.actor !== "undefined") {
   actor = options.actor;
} else if (this.actor !== null) {
   actor = this.actor;
}

and jslint complains of the first line that

"Unexpected 'typeof'. Use '===' to compare directly with undefined."

but the complaint makes little sense to me.

Why is typeof unexpected?
Why is it asking compare the string return with a primitive?

Andrew Poulos

[toc] | [next] | [standalone]


#19838

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-05-20 13:40 +0300
Message-ID<kncubd$i57$1@dont-email.me>
In reply to#19837
2013-05-20 12:59, Andrew Poulos wrote:

> if (typeof options.actor !== "undefined") {
[...]
> and jslint complains of the first line that
>
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>
> but the complaint makes little sense to me.

Since the only value of type "undefined" is the one denoted by the 
variable undefined, the test

typeof options.actor !== "undefined"

is unnecessary complicated, since you can use

options.action !== undefined

instead.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

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


#19839

FromEric Bednarz <bednarz@fahr-zur-hoelle.org>
Date2013-05-20 13:34 +0200
Message-ID<m2r4h16g1i.fsf@nntp.bednarz.nl>
In reply to#19838
"Jukka K. Korpela" <jkorpela@cs.tut.fi> writes:

> 2013-05-20 12:59, Andrew Poulos wrote:
>
>> if (typeof options.actor !== "undefined") {
> [...]
>> and jslint complains of the first line that
>>
>> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>>
>> but the complaint makes little sense to me.

OK.

> Since the only value of type "undefined" is the one denoted by the
> variable undefined,

Property, not variable, and that's not necessarily true, either.

> the test
>
> typeof options.actor !== "undefined"
>
> is unnecessary complicated, since you can use
>
> options.action !== undefined

This, however, is unnecessarily brittle since the undefined property of
the global object is writable in ES < 5. It is also overly verbose.

options.action !== void 0;

(JSLint won't like that either, of course.)

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


#19841

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-20 14:23 +0200
Message-ID<15066019.Edmlg4qf5b@PointedEars.de>
In reply to#19839
Eric Bednarz wrote:

> "Jukka K. Korpela" <jkorpela@cs.tut.fi> writes:
>> the test
>>
>> typeof options.actor !== "undefined"
>>
>> is unnecessary complicated, since you can use
>>
>> options.action !== undefined
> 
> This, however, is unnecessarily brittle since the undefined property of
> the global object is writable in ES < 5.

ACK.  And in earlier implementations there was no “undefined” property, 
causing this to throw (the equivalent of) a ReferenceError.

> It is also overly verbose.

In what way?
 
> options.action !== void 0;

I would call *that* overly verbose.  For backwards compatibility, I have 
occasionally declared

  var undefined;

However, nowadays the “!==” and “===” operators and the “undefined” property 
of the Global Object can generally be considered safe language features.  
Only a caveat with the property value and host objects must remain for the 
time being.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#19961

FromEric Bednarz <bednarz@fahr-zur-hoelle.org>
Date2013-05-26 17:40 +0200
Message-ID<m2zjvhbvg5.fsf@nntp.bednarz.nl>
In reply to#19841
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Eric Bednarz wrote:
>
>> "Jukka K. Korpela" <jkorpela@cs.tut.fi> writes:

>>> the test
>>>
>>> typeof options.actor !== "undefined"
>>>
>>> is unnecessary complicated, since you can use
>>>
>>> options.action !== undefined

>> It is also overly verbose.
>
> In what way?

In the three-characters-more way (as far as I am concerned, the typeof
operator is safe to use and easy to read, so I was just being
sarcastic).

>> options.action !== void 0;
>
> I would call *that* overly verbose.

I don't, I just call it unreadable. :-)

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


#19840

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-20 13:48 +0200
Message-ID<6079239.H4DsMt3hnF@PointedEars.de>
In reply to#19837
Andrew Poulos wrote:

> A fragment of some code I've been given has this
> 
> if (typeof options.actor !== "undefined") {
>    actor = options.actor;
> } else if (this.actor !== null) {
>    actor = this.actor;
> }
> 
> and jslint complains of the first line that
> 
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
> 
> but the complaint makes little sense to me.
> 
> Why is typeof unexpected?

Probably because Douglas Crockford is wrong.  Prior to ECMAScript Ed. 5, 
when jslint was released, the “undefined” property of the Global Object was 
already writable, and it was safer to compare against the result of the 
“typeof” operation.  Nowadays, and especially with host objects, there are 
insufficient good reasons to choose a different approach.

> Why is it asking compare the string return with a primitive?

Crockford suggests that you write

  if (options.actor !== undefined) {

instead.  But this can only be recommended if you can assume that 
“undefined” is read-only.

Ignore jslint.

That said, this should probably be written

  var actor = options.actor || this.actor;

See also jsx.array.createComparator().

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#19842

FromStefan Weiss <krewecherl@gmail.com>
Date2013-05-20 14:44 +0200
Message-ID<knd5r1$9cj$1@news.albasani.net>
In reply to#19837
On 2013-05-20 11:59, Andrew Poulos wrote:
> "Unexpected 'typeof'. Use '===' to compare directly with undefined."

Deja vu:
https://groups.google.com/d/topic/comp.lang.javascript/tJuCNzQEbb4

- stefan

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


#19843

FromAndrew Poulos <ap_prog@hotmail.com>
Date2013-05-20 23:17 +1000
Message-ID<Houdnb2c5rfFvwfMnZ2dnUVZ_jOdnZ2d@westnet.com.au>
In reply to#19842
On 20/05/2013 10:44 PM, Stefan Weiss wrote:
> On 2013-05-20 11:59, Andrew Poulos wrote:
>> "Unexpected 'typeof'. Use '===' to compare directly with undefined."
>
> Deja vu:
> https://groups.google.com/d/topic/comp.lang.javascript/tJuCNzQEbb4

Dang, I asked the identical question 4 months ago (which was duly 
answered). It was only 8pm local time when I posted so I can't use the 
excuse of lack of sleep.

Andrew Poulos

[toc] | [prev] | [standalone]


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


csiph-web