1623 }
1624
1625 if (! String.prototype.includes) {
1626 String.prototype.includes = _includes; 1627 }
1628
1629 /**
1617 // .trim() polyfill
1618 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim
1619 if (!String.prototype.trim) {
1620 String.prototype.trim = function () { 1621 return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); 1622 }; 1623 }
1624
1625 if (! String.prototype.includes) {
1611 }
1612
1613 if (! Array.prototype.includes) {
1614 Array.prototype.includes = _includes; 1615 }
1616
1617 // .trim() polyfill
1611 }
1612
1613 if (! String.prototype.includes) {
1614 String.prototype.includes = _includes; 1615 }
1616
1617 /**
1605 // .trim() polyfill
1606 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim
1607 if (!String.prototype.trim) {
1608 String.prototype.trim = function () { 1609 return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); 1610 }; 1611 }
1612
1613 if (! String.prototype.includes) {
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 });