Re: [whatwg/dom] Add NodeList.setAttributes (#895)

I think a separate static method would be better, as it can then operate on arrays of elements, not just nodelists. Many libraries and helper methods today return arrays for operations, and AFAIK there is no way to convert an array to a `NodeList`, since it is not constructible.

Many bulk operations are commonly needed:
- Multiple attributes
- Multiple properties
- Multiple events
- All of the above on multiple elements

Performance is only one aspect, readability and maintainability is far bigger, which is why this pattern is so popular and has been since 2006 (jQuery popularized it I believe) despite the lack of any performance improvement.

Consider this:

```js
let audio = document.createElement("audio");
audio.setAttribute("src", "rain.mp3");
audio.setAttribute("autoplay", "");
audio.setAttribute("start", "00:00:00.00");
audio.setAttribute("loopstart", "00:00:00");
audio.setAttribute("end", "00:01:00");
audio.setAttribute("playcount", "100");
audio.setAttribute("controls", "");
```

and this:

```js
let audio = document.createElement("audio");
DOM.setAttributes(audio, {
 src: "rain.mp3",
 autoplay: "",
 start: "00:00:00.00",
 loopstart: "00:00:00",
 end: "00:01:00",
 playcount: "100",
 controls: ""
});
```

or even:

```js
let audio = document.createElement("audio", {
 attributes: {
  src: "rain.mp3",
  autoplay: "",
  start: "00:00:00.00",
  loopstart: "00:00:00",
  end: "00:01:00",
  playcount: "100",
  controls: ""
 }
});
```

Which one is easier to read and maintain?

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

Received on Friday, 13 November 2020 15:43:40 UTC