category | created | tags | title |
---|---|---|---|
Practice |
2021-02-22 |
JavaScript |
Do not add custom methods to primitive objects |
It is not recommended to add a custom method to primitive objects such as Array
, Boolean
, Number
, String
, etc.
Since the for ... in
statement loops over the enumerable properties, it will include new methods which are added to the prototype.
Array.prototype.isEmpty = function () {
return (this.length = 0);
};
const a = ['cat', 'dog', 'mouse'];
for (let i in a) {
console.log(i); // '0', '1', '2', 'isEmpty'
}