js原型链

12次阅读

共计 958 个字符,预计需要花费 3 分钟才能阅读完成。

图片引自 https://juejin.im/post/5b44a4…
看到这张图的第一感觉是

对于完整的原型链大家平常可能接触的并不多,容易忘记,单看图并不是很好理解,以下是在控制台的输出首先要知道_proto_的作用,__proto__是一个对象拥有的内置属性(请注意:prototype 是函数的内置属性,__proto__是对象的内置属性),是 JS 内部使用寻找原型链的属性。指向自身构造函数的原型, 同理调用 prototype 可以看作调用者是函数,__proto__调用者可以看成是对象以 Object 开始直到回到 Object
Object.constructor
=> ƒ Function() { [native code] } 等同于 function Function {[native code] }
Object 的构造函数其实是一具名函数 Function

Object.constructor.constructor
=> ƒ () { [native code] } 等同于匿名函数 function(){ [native code] }

Object.constructor.__proto__ == Object.constructor.prototype
=> ƒ () { [native code] }

Object.constructor.__proto__.constructor
=> ƒ Function() { [native code] } 等同于 function Function {[native code] }

Object.constructor.__proto__.__proto__
=> Object.prototype 如下
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

Object.constructor.__proto__.__proto__.constructor
=> Object 又回到了自己
Object.constructor.__proto__.__proto__.__proto__
=> Null
至于程序为什么这么设计,这里就不再讨论了,有一点 一开始学习记住是什么比为什么更重要,因为谁也解释不了 1 + 1 为什么等于二,记忆很重要

正文完
 0