Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32376
| Path | csiph.com!2.us.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" |
| Date | Fri, 27 Jan 2017 13:04:03 +0100 |
| Organization | PointedEars Software (PES) |
| Lines | 41 |
| Message-ID | <11701968.uLZWGnKmhe@PointedEars.de> (permalink) |
| References | <o6eo1r$ifl$1@shakotay.alphanet.ch> <878tpwzqct.fsf@bsb.me.uk> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1485518643 11153 eJwFwQkBwDAIA0BLdJBA5fAU/xJ2B+VhuxE0LPY54EOZ3FxEmNxhmJ/9SivT0K/L86rsI+UHIw0Rag== (27 Jan 2017 12:04:03 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Fri, 27 Jan 2017 12:04:03 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-NNTP-Posting-Host | eJwFwQkBgAAIA8BKQ2BgHPn6R/DOlcIOo9P8/BLKfANqGtf8rrwDe4oZgfRIPjWc3loVww8PkBEh |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkP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xref | csiph.com comp.lang.javascript:32376 |
Show key headers only | 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
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