In JavaScript, you can extend any object, including builtin or "native" objects. Sometimes people change the behavior of these native objects in ways that break the assumptions made about them in other parts of the code.
// seems harmless
Object.prototype.extra = 55;
// loop through some userIds
const users = {
"123": "Stan",
"456": "David"
};
// not what you'd expect
for (const id in users) {
console.log(id); // "123", "456", "extra"
}
In the above example, a common suggestion to avoid this problem would be to wrap the inside of the for
loop with users.hasOwnProperty(id)
.
Object.prototype.a = "a";
Object.defineProperty(Array.prototype, "times", { value: 999 });