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


Groups > comp.lang.javascript > #19840

Re: jslint and typeof

Message-ID <6079239.H4DsMt3hnF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-05-20 13:48 +0200
Subject Re: jslint and typeof
Newsgroups comp.lang.javascript
References <kr2dnRlKLtGEaQTMnZ2dnUVZ_g6dnZ2d@westnet.com.au>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web