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


Groups > comp.lang.javascript > #32376

Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)"

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)"
Date 2017-01-27 13:04 +0100
Organization PointedEars Software (PES)
Message-ID <11701968.uLZWGnKmhe@PointedEars.de> (permalink)
References <o6eo1r$ifl$1@shakotay.alphanet.ch> <878tpwzqct.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> The test ~[2, 3].indexOf(n) is a tricky way of testing if n is 2 or 3.
> All the valid indexes (0 and 1) have bit-negations that are non-zero and
> hence "true".  The return that indicates that n is not present (-1)
> bit-negates to 0 which is taken to be false.
> 
> I'd write n == 2 || n == 3

Keep in mind that you need to write “===” for equivalence to .indexOf().

> but if there were lots of values to consider I'd probably write [2, 3, 8,
> 9, 77, 102].indexOf(n) >= 0 instead.

For few values, and if they are Number values with proper string 
representation, you can also write

  n in {2: 1, 3: 1}

instead (“1” serving as the shortest character sequence for a property 
value; any value works).

“||” is most backwards-compatible, but requires the most space.

With String values, you have to create the object with Object.create(null) 
(or equivalent) unless you can be sure that names of built-in properties of 
Object instances and their prototype could never be matched.  However, both 
Object.create() and Array.prototype.indexOf() were introduced in ECMAScript 
Ed 5., and you can emulate both to support the desired behavior.  As of 
ECMAScript 2016, you can also use Map instances to avoid name collisions and 
support non-string values (as keys).

Either approach is useful if you need to test repeatedly whether a value is 
contained in a set of non-unique values, where repeated linear search is 
comparably inefficient.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
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

understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-27 07:06 +0100
  Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-27 11:36 +0000
    Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-27 13:04 +0100
    Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-27 18:00 +0100
      Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-28 10:41 +0100
        Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-28 15:33 +0000
    Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" Une Bévue <unbewusst.sein@fai.invalid> - 2017-01-29 09:12 +0100

csiph-web