Hint
Array.prototype, String.prototype etc β extending them affects ALL instances; almost always a bad idea
All built-in types (Array, String, Object, Functionβ¦) have prototypes with their methods. Every array shares Array.prototype.
// How it works β built-in prototype chain
const arr = [1, 2, 3];
// arr.__proto__ === Array.prototype β
// Array.prototype.__proto__ === Object.prototype β
// All arrays share Array.prototype methods
arr.map === Array.prototype.map; // true β same reference
// Checking native prototype
Array.prototype.includes; // function β built-in
String.prototype.padStart; // function β built-in
// β Bad: extending native prototypes (prototype pollution risk)
Array.prototype.last = function() { return this[this.length - 1]; };
// Now EVERY array in ALL your code + libraries has .last β collisions!
// β Famous historical mistake: Prototype.js library
// It extended Array.prototype and broke all for...in loops on arrays
// β
If you must extend (only in polyfills β check first)
if (!Array.prototype.myMethod) { // always guard with existence check
Array.prototype.myMethod = function() { ... };
}
// β
Better: use utility functions or subclassing
class SuperArray extends Array {
last() { return this[this.length - 1]; }
}
const sa = new SuperArray(1, 2, 3);
sa.last(); // 3 β only SuperArray instances affected