Message from JavaScript discussions
July 2017
— I just went to use it and was shocked!
I made this:
// `Array.values()` Polyfill
// Returns a new Array Iterator object that contains the values for each index in the array.
if (!Array.prototype.hasOwnProperty("values")) {
Object.defineProperty(Array.prototype, "values", {
enumerable: false,
value: function* () {
for (var value of this) {
yield value;
}
}
});
}
— SO simple... and yet they disabled this... Whyyyyyy
— Weird
— Didn't know that was a thing
— But I normally use for..of anyway
— I need it because the functions in cdaTemplate are polymorphic, can use Arrays or NodeLists, and NodeLists have values()
— Instead of branching or setting up new boilerplate to handle the Arrays, I can just use the same code now :D
— Specifically, insertTemplate
accepts a variable, destElems
which can either be a 1-item array like this: [Node]
or a NodeList full of Nodes
—
var destElems = [document.querySelector(destSel)];
versus
var destElems = document.querySelectorAll(destSel);
— And, then in use:
// Iterate through the destinations for insertion
for (var destElem of destElems.values()) { ...
— Ultimate root cause of the need is due to NodeList's abstract nature, it requires you to use an iterator because it is a "view" on the DOM which is a graph structure, and does not "contain" any Nodes itself