v154 · websocket options

Why a dictionary at all

The dictionary adds no behaviour on its own, which makes "why bother?" a fair question. The answer is detectability: you can ask a dictionary whether the browser understands a member, and you cannot ask that of a positional argument at all.

Feature detection, both shapes

Dictionary: ask with a getter

let read = false;
new WebSocket(url, {
  get protocols() { read = true; return []; }
});
// `read` is now a definite answer.

not run

Positional: no way to ask

// Suppose a third argument were added.
new WebSocket(url, ["echo"], { future: true });
// An older browser ignores it silently.
// There is nothing to inspect, and no error.

not run

Press the button.

What each shape does as options accumulate

Adding three hypothetical options to each API shape
propertypositional argumentsdictionary

This is not hypothetical for long: targetAddressSpace ships in the same release and is a dictionary member. Adding it as a third positional argument would have meant every caller passing undefined for protocols they did not want, and no way to tell whether it had any effect.

see also