Built-in Prototypes
Built-in Prototypes
Section titled “Built-in Prototypes”Introduction
Section titled “Introduction”All built-in objects in JavaScript have prototypes that provide their methods. Understanding the built-in prototype chain helps you know where methods come from.
The Prototype Hierarchy
Section titled “The Prototype Hierarchy”// Array → Array.prototype → Object.prototype → null// Function → Function.prototype → Object.prototype → null// String → String.prototype → Object.prototype → nullArray.prototype
Section titled “Array.prototype”All array methods live on Array.prototype:
const arr = [1, 2, 3];
// These methods are defined on Array.prototype:// push, pop, map, filter, reduce, find, sort, forEach, etc.
console.log(arr.__proto__ === Array.prototype); // trueconsole.log(Array.prototype.__proto__ === Object.prototype); // trueFunction.prototype
Section titled “Function.prototype”function greet() {}// call, apply, bind, etc. are on Function.prototype
console.log(greet.__proto__ === Function.prototype); // trueconsole.log(Function.prototype.__proto__ === Object.prototype); // trueObject.prototype
Section titled “Object.prototype”The top of the prototype chain (for most objects):
// Methods all objects inherit:// toString, hasOwnProperty, valueOf, isPrototypeOf, propertyIsEnumerable
const obj = {};console.log(obj.__proto__ === Object.prototype); // trueconsole.log(Object.prototype.__proto__); // null — end of chainWhy This Matters
Section titled “Why This Matters”const arr = [3, 1, 2];
// arr inherits from Array.prototypearr.sort(); // Array.prototype.sortconsole.log(arr); // [1, 2, 3]
// arr also inherits from Object.prototype (via Array.prototype)console.log(arr.hasOwnProperty(0)); // Object.prototype.hasOwnPropertyExtending Built-in Prototypes (⚠️ Avoid!)
Section titled “Extending Built-in Prototypes (⚠️ Avoid!)”// ❌ Bad practice — don't modify built-in prototypesArray.prototype.first = function() { return this[0]; };// Now ALL arrays have this method — potential conflicts!
// ❌ Even worse — overwriting existing methodsArray.prototype.push = function() { console.log('push disabled'); // Breaks all arrays!};Checking Where a Method Comes From
Section titled “Checking Where a Method Comes From”const arr = [1, 2, 3];
// Check if it's an own propertyconsole.log(arr.hasOwnProperty('map')); // false — inherited
// Check the prototypeconsole.log('map' in arr); // true — found in chainconsole.log(arr.map === Array.prototype.map); // true — same referenceSummary
Section titled “Summary”| Constructor | Prototype | Key Methods |
|---|---|---|
Array | Array.prototype | push, pop, map, filter, reduce |
Function | Function.prototype | call, apply, bind |
String | String.prototype | charAt, slice, toUpperCase |
Number | Number.prototype | toFixed, toPrecision |
Object | Object.prototype | toString, hasOwnProperty |
- All built-in prototypes ultimately inherit from
Object.prototype - The chain ends at
Object.prototype.__proto__=null - Never modify built-in prototypes (risks global conflicts)