Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32363 > unrolled thread
| Started by | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| First post | 2017-01-27 07:06 +0100 |
| Last post | 2017-01-29 09:12 +0100 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-01-27 07:06 +0100 |
| Subject | understanding the expression "ws && ~[2, 3].indexOf(ws.readyState)" |
| Message-ID | <o6eo1r$ifl$1@shakotay.alphanet.ch> |
The expression :
ws && ~[2, 3].indexOf(ws.readyState)
is within a test :
if (ws && ~[2, 3].indexOf(ws.readyState)) {
connected = false;
init(openCallback, closeCallback);
}
ws being an instance of WebSocket created by :
ws = new WebSocket(url);
when WebSocket is ready the value of ws.readyState is 1.
the test "if (ws && ~[2, 3].indexOf(ws.readyState)) {"
re-init a connection to the websocket when ws.readyState is not equal to 1
(as far as i understand well)
i did some tests :
~[2, 3].indexOf(1); // gives 0
~[2, 3].indexOf(2); // gives -1
~[2, 3].indexOf(3); // gives -2
~ is a bitwise NOT operator, for example :
~ 5 being ~0101 gives 1010
(in fact the numbers aren't 4 bits wide rather 32 bits in JavaScript)
I'd like to decompose the above expression :
ws && ~[2, 3].indexOf(ws.readyState)
ws is a kind of existance test "true" when ws is not null nor undefined ;
~[2, 3].indexOf(ws.readyState) equals 0 (logical false) when WebSocket
is in ready state.
then clearly the test is false when ws.readyState = 1, no attempt to
reconnect using init(...).
however when ws.readyState is of 2 or 3 what is the logical value of -1
or -2 ?
does negative numbers have an equivalent logical value of false ?
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-01-27 11:36 +0000 |
| Message-ID | <878tpwzqct.fsf@bsb.me.uk> |
| In reply to | #32363 |
Une Bévue <unbewusst.sein@fai.invalid> writes: > The expression : > ws && ~[2, 3].indexOf(ws.readyState) <snip> > i did some tests : > > ~[2, 3].indexOf(1); // gives 0 > ~[2, 3].indexOf(2); // gives -1 > ~[2, 3].indexOf(3); // gives -2 <snip> > ~[2, 3].indexOf(ws.readyState) equals 0 (logical false) when WebSocket > is in ready state. > > then clearly the test is false when ws.readyState = 1, no attempt to > reconnect using init(...). > > however when ws.readyState is of 2 or 3 what is the logical value of > -1 > or -2 ? > > does negative numbers have an equivalent logical value of false ? Any non-zero number (other than NaN) is considered to be true in a Boolean context. 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 but if there were lots of values to consider I'd probably write [2, 3, 8, 9, 77, 102].indexOf(n) >= 0 instead. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-01-27 13:04 +0100 |
| Message-ID | <11701968.uLZWGnKmhe@PointedEars.de> |
| In reply to | #32374 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-01-27 18:00 +0100 |
| Message-ID | <o6fube$62g$1@shakotay.alphanet.ch> |
| In reply to | #32374 |
Le 27/01/2017 à 12:36, Ben Bacarisse a écrit : > Any non-zero number (other than NaN) is considered to be true in a > Boolean context. ok, fine, it's what I expected. > 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. ok, thanks clear enough. > I'd write n == 2 || n == 3 but if there were lots of values to consider > I'd probably write [2, 3, 8, 9, 77, 102].indexOf(n) >= 0 instead. Yes, sure, then I've to investigate what could be the different values of #readyState.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-01-28 10:41 +0100 |
| Message-ID | <XnsA70B6CBA8F9BCeejj99@194.109.6.166> |
| In reply to | #32386 |
=?UTF-8?Q?Une_B=c3=a9vue?= <unbewusst.sein@fai.invalid> wrote on 27 Jan 2017 in comp.lang.javascript: > Le 27/01/2017 … 12:36, Ben Bacarisse a ‚crit : >> Any non-zero number (other than NaN) is considered to be true in a >> Boolean context. Please reconsider: alert(0 == false) // true alert(0 === false) // false alert(17 == true) // false ??????????? alert(true == 17) // false ??????????? alert(!!17) // true > ok, fine, it's what I expected. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-01-28 15:33 +0000 |
| Message-ID | <874m0jurl9.fsf@bsb.me.uk> |
| In reply to | #32401 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: <snip> >> Le 27/01/2017 … 12:36, Ben Bacarisse a ‚crit : >>> Any non-zero number (other than NaN) is considered to be true in a >>> Boolean context. > > Please reconsider: What should I be re-considering? > alert(0 == false) // true > alert(0 === false) // false > > alert(17 == true) // false ??????????? > alert(true == 17) // false ??????????? > alert(!!17) // true There is only one example of a non-zero number in a Boolean context there and it behaves as I described. Maybe I should have been explicit about what I meant by a Boolean context. It's where the abstract toBoolean operation is used in the language specification. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Une Bévue <unbewusst.sein@fai.invalid> |
|---|---|
| Date | 2017-01-29 09:12 +0100 |
| Message-ID | <o6k865$caa$1@shakotay.alphanet.ch> |
| In reply to | #32374 |
Le 27/01/2017 à 12:36, Ben Bacarisse a écrit : > I'd write n == 2 || n == 3 but if there were lots of values to consider > I'd probably write [2, 3, 8, 9, 77, 102].indexOf(n) >= 0 instead. According to <https://developer.mozilla.org/en-US/docs/Web/API/WebSocket> the values of readyState are : Constant Value Description CONNECTING 0 The connection is not yet open. OPEN 1 The connection is open and ready to communicate. CLOSING 2 The connection is in the process of closing. CLOSED 3 The connection is closed or couldn't be opened. Then I only test #readyState verssus 1...
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web