I apologize if this is the wrong place to discuss this.
Can we make this happen (better reading), I tried fixing indentation on here):
DOMStringMap.prototype.__proto__ = {
keys() {
let keys = Object.keys(this),
count = 0;
return {
next() {
if(count == keys.length) return { value: undefined, done: true };
return { value: keys[count++], done: false };
}
}
},
values() {
let values = Object.keys(this).map(key => this[key], this),
count = 0;
return {
next() {
if(count == values.length) return { value: undefined, done: true };
return { value: values[count++], done: false };
}
}
},
entries() {
let keys = Object.keys(this),
values = keys.map(key => this[key], this),
count = 0;
return {
next() {
if(count == keys.length) return { value: undefined, done: true };
return { value: { key: keys[count], value: values[count++] }, done: false };
}
}
},
[Symbol.iterator]: DOMStringMap.prototype.values //Or perhaps .entries
}
Now the [Symbol.iterator]: DOMStringMap.prototype.values
is to make it iterable in for of
loops.
Can someone please explain how the following works:
for(var c of document.body.classList) console.log(c);
Meaning how is the above possible if it’s a DOMStringList
and DOMStringList.prototype[Symbol.iterator]
is undefined
.
Yet the above doesn’t work with DOMStringMap
example would be document.body.dataset