3 ms·
You can extend HtmlElementCollection prototype with any methods you like. (it's NodeList, btw, what's returned from querySelectorAll)
by megous 4y ago
You can extend HtmlElementCollection prototype with any methods you like. (it's NodeList, btw, what's returned from querySelectorAll)
- strbean 4y agoMy understanding is that mutating built in prototypes is very bad. Not just for interop reasons, but because doing so really hinders many optimizations javascript engines perform.
- cogman10 4y agoAdding methods to prototypes is effectively free. The only time mutating a builtin prototype has negative consequences is when you replace a builtin function with your own thing. All javascript engines (that I'm aware of) JIT at the method level so there's not really a performance impact modifying a prototype.
- strbean 4y agoAh, I think this is what I was thinking of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/The_performance_hazards_of_prototype_mutation https://developer.mozilla.org/en-US/docs/Web/JavaScript/The_... It seems this only pertains specifically to overwriting the __proto__ of an existing object (the is the prototype of other things).
- simion314 4y agoThere is an issue you need to be aware , be careful if you hide the fact that this collection is "live". I mean this part from the docs >An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed. For this reason it is a good idea to make a copy (eg. using Array.from) to iterate over if adding, moving, or removing nodes. So you need something that will also be efficient and correct, not a 5 minutes implementation.
- megous 4y agoSame issue applies if you mutate array you're iterating over during iteration using those Array.prototype functions. This is just a general issue with any data structure.