Bug 16223 - Change .readyState to use string values rather than numeric constants
Change .readyState to use string values rather than numeric constants
Product: WebAppsWG
Classification: Unclassified
Component: WebSocket API (editor: Ian Hickson)
PC All
: P2 normal
: ---
Assigned To: Ian 'Hixie' Hickson
Depends on:
  Show dependency treegraph
Reported: 2012-03-04 00:12 UTC by Jonas Sicking
Modified: 2012-03-13 03:57 UTC (History)
3 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Jonas Sicking 2012-03-04 00:12:39 UTC
Numeric constants sucks when used in javascript. Code like

if (ws.readyState == WebSocket.CONNECTING) { ... }

is both harder to read and harder to type compared to

if (ws.readyState == "connecting") { ... }

All the uppercase letters are a pain to type and and stick out like a sore thumb.

Additionally there's a very real risk that people will write code like

if (ws.readyState == 0) { ... }

since '0' is so much easier to write than 'WebSocket.CONNECTING'.

It's even likely that comparing to a string is faster than comparing to WebSocket.CONNECTING since the latter involves a property lookup.
Comment 1 Ian 'Hixie' Hickson 2012-03-13 03:57:26 UTC
While I agree with the design principle, I think it's not a big enough win to be worth the churn here. People complain all the time that we keep changing things — we should only do it when we have a truly compelling reason (e.g. a security problem).

In practice, little non-debug code will likely end up using websocket.readyState anyway. It's easier to use the events. It was mostly added for consistency with XMLHttpRequest, which also uses numbers — and people don't generally find it onerous to remember which readyState is which for that API.