Wrap the body of a for-in loop in an if statement with a hasOwnProperty guard
224 }
225
226 var d = document.createElement(tag)
227 for (var k in opt.attr)228 d.setAttribute(k, opt.attr[k])229
230 var p = document.querySelector(opt.append)
231 if (!p)
Description
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain.
This behavior can lead to unexpected keys in your for loop.
Bad Practice
for (const key in foo) {
doSomething(key);
}
Recommended
for (const key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
for (const key of Object.keys(foo)) {
doSomething(key);
}