为什么Object._proto_引用Function.prototype?

时间:2018-07-25 00:28:31

标签: javascript prototype

我正在下面的对象和函数之间的关系图中查看图表,我想知道为什么Object。 proto 引用Function.prototype?

enter image description here

1 个答案:

答案 0 :(得分:1)

[memtest]> cargo run Compiling memtest v0.1.0 (file:///home/icarruthers/memtest) Finished dev [unoptimized + debuginfo] target(s) in 0.28s Running `target/debug/memtest` [memtest]> valgrind target/debug/memtest ==18808== Memcheck, a memory error detector ==18808== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==18808== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==18808== Command: target/debug/memtest ==18808== ==18808== ==18808== HEAP SUMMARY: ==18808== in use at exit: 0 bytes in 0 blocks ==18808== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==18808== ==18808== All heap blocks were freed -- no leaks are possible ==18808== ==18808== For counts of detected and suppressed errors, rerun with: -v ==18808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 是指对象的直接原型祖先。举一个简单的例子,__proto__obj的直接祖先:

obj2

在您的图表中,它显示了从函数到let obj = {} // make obj2 with obj parent let obj2 = Object.create(obj) obj === obj2.__proto__ // true Function.prototype的原型链。您可以像链条一样跟随它:

Object.prototype

在您的示例中,function test(){} console.log(test.__proto__ === Function.prototype) console.log(test.__proto__.__proto__ === Object.prototype) // alternatively: Object.getPrototypeOf(test) // Function.prototypeObject.__proto__,因为Function.prototype是构造函数,而ObjectString是函数。但这与Number不同,后者是另一个对象的Object.prototype属性指向的对象:

__proto__