一步步 深入解析 原型链的奥秘
一切的开始从人类开始说起
function person(){};
const Adam = new person();
// 函数实例化 出 Adam(亚当)这个对象 利用关键字new
复制代码
console.log(Adam.__proto__);
console.log(Adam.__proto__.constructor) // function person(){}
console.log(person.prototype) //Adam原型对象
console.log(person.prototype === Adam.__proto__) //true
// 实例的__proto__指的是 创造该实例的原型对象
复制代码
万物皆对象
console.log(Adam.__proto__.__proto__) // Object 原型对象
console.log(Adam.__proto__.__proto__.__proto__)//null
console.log(Adam.__proto__.__proto__.constructor)//function Object(){ [native code] }
console.log(Adam.__proto__.__proto__.constructor.prototype)//Object原型对象
复制代码
- 只有函数才有 prototype 指向原型对象
- 万物都是对象
- 原型对象利用 constructor 构造函数 创建实例
- [native code] 这里是底层 C 并不对外开放
一切普通的函数都是由大 Function 来制造来的
console.log(person.__proto__)//Function 原型对象
console.log(Adam.__proto__.__proto__.constructor.__proto__ === person.__proto__)
//true 这两个都是函数
// 所以 __proto__皆指向 大Function的原型对象
console.log(person.__proto__.__proto__)
//万般皆是命 ,全都是对象
复制代码
特殊的大 Function
console.log(person.__proto__.constructor) //function Function(){ [native code] }
console.log(person.__proto__.constructor.prototype)
// 大Function 的原型对象
复制代码
注意这里
console.log(person.__proto__.constructor.__proto__ === person.__proto__) // true
- 大Function的 __proto__又指☞回了 大Function的原型对象
##总结:
- 只有function 才存在 prototype
- 原型对象 存在着 constructor
- 实例的 __proto__指向 原型对象
- 大Function的 proto 指回了 Function原型对象
- 普通函数的 proto 指向 Function原型对象