This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 16223 - Change .readyState to use string values rather than numeric constants
Summary: Change .readyState to use string values rather than numeric constants
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: WebSocket API (editor: Ian Hickson) (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: Ian 'Hixie' Hickson
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-04 00:12 UTC by Jonas Sicking (Not reading bugmail)
Modified: 2012-03-13 03:57 UTC (History)
3 users (show)

See Also:


Attachments

Description Jonas Sicking (Not reading bugmail) 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.