[whatwg/dom] Proposal: A new way to do feature detection for members (Issue #1181)

Web APIs are relying more and more on dictionaries. And doing feature detection of such members requires ugly and [unnecessary complicated code](https://github.com/whatwg/webidl/issues/107). Following https://github.com/WICG/EventListenerOptions/issues/31, https://github.com/whatwg/dom/issues/491, and many more over the years, I'm proposing WebIDL allows us to expose interfaces under a specific "namespace/scope" to avoid polluting the global scope.

### Current state for an API called `MyAPI`
1. A dictionary makes it unnecessarily hard to detect its members.
```webidl
dictionary MyDictionaryOptions {
  boolean foo;
};
```
2. An interface pollutes global scope as `window.MyDictionaryOptions` now unnecessarily exists. 
```webidl
[Exposed=Window]
interface MyDictionaryOptions {
  boolean foo;
}
```
### Proposal
Use interfaces instead of dictionaries when possible and expose them under a specific member of the API.
```webidl
[Exposed=Window.MyAPI] // <-- NEW!
interface MyDictionaryOptions {
  boolean foo;
}
```
```js
if ("foo" in window.MyAPI.MyDictionaryOptions) {
  // 1. It's easy to detect foo exists in MyDictionaryOptions 
  // 2. Detecting it does not pollute global scope
  // 3. It does not require executing some methods to test it.
}
```

What do you folks think about this?


-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1181
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1181@github.com>

Received on Monday, 3 April 2023 07:18:20 UTC