Object.getPrototypeOf只能用于实例对象,不能用于构造函数;数组、字符串等实例的原型分别是Array.prototype、String.prototype等,原始值会自动装箱,null/undefined会报错。

可以直接用 Object.getPrototypeOf 获取内置对象(如 Array、String、Date 等)的原型,但要注意:传入的必须是**实例对象**,不能直接传构造函数本身(否则得到的是函数的原型,即 Function.prototype)。
获取数组实例的原型
数组实例的原型是 Array.prototype,它继承自 Object.prototype:
-
Object.getPrototypeOf([1, 2, 3]) === Array.prototype→ true -
Object.getPrototypeOf(Array.prototype) === Object.prototype→ true -
Object.getPrototypeOf(Object.prototype) === null→ true(原型链终点)
获取字符串、数字、布尔值等原始包装对象的原型
原始值(如 "hello")不是对象,但调用 Object.getPrototypeOf 会自动装箱为包装对象:
-
Object.getPrototypeOf("hello") === String.prototype→ true -
Object.getPrototypeOf(42) === Number.prototype→ true -
Object.getPrototypeOf(true) === Boolean.prototype→ true
⚠️ 注意:null 和 undefined 无法装箱,传入会抛出 TypeError。
避免常见错误:别对构造函数本身调用
误写:Object.getPrototypeOf(Array) 返回的是 Function.prototype,因为 Array 是函数,不是数组实例:
-
Object.getPrototypeOf(Array) === Function.prototype→ true - 这不是你想要的“数组的原型”,而是“构造函数的原型”
- 正确做法始终基于实例,例如
new Array()或[]
检查内置原型的实用方式
结合 === 或 isPrototypeOf 验证继承关系:
-
Array.prototype.isPrototypeOf([1, 2])→ true -
Object.prototype.isPrototypeOf("test")→ true(所有对象最终继承它) -
console.log(Object.getPrototypeOf(new Date()))可查看Date.prototype的结构
不复杂但容易忽略。

















