JavaScript中Object.prototype的层级是什么?及代码输出解析请求
Hey there! Let's tackle your JavaScript questions step by step, nice and clear.
In JavaScript, Object.prototype sits at the very top of the prototype chain for almost all objects—with the only exception being null. Here's how the hierarchy breaks down:
- Plain objects (like
const obj = {}) have their__proto__pointing directly toObject.prototype. - Function objects (including built-in constructors like
Object,Array, or your custom functions) first inherit fromFunction.prototype, and thenFunction.prototype's__proto__points toObject.prototype. - Finally,
Object.prototype.__proto__isnull—this is the end of the prototype chain; there's nothing above it to inherit from.
To visualize it simply:
Your Custom Object → Object.prototype → null Object (the constructor function) → Function.prototype → Object.prototype → null
Let's go through each log statement and why it outputs what it does:
First log: console.log(Object);
Object is a built-in constructor function provided by JavaScript. When you log it directly, you see ƒ Object() { [native code] }—this is just the browser/engine's way of showing you that this is a native (engine-implemented) function, not one written in JavaScript itself.
Second log: console.log(Object.__proto__);
Remember, Object is a function, not a plain object. All functions in JavaScript inherit from Function.prototype. When you log Object.__proto__, you're seeing Function.prototype itself, which is represented as ƒ () { [native code] }—it's a special empty function that serves as the prototype for all function objects.
Third log: console.log(obj.prototype);
Here's the key point: only function objects have a prototype property. The variable obj is a plain object (created with {}), so it doesn't have a prototype attribute at all. That's why this log outputs undefined.
Quick note to avoid confusion: Every object (including functions) has a __proto__ property (or you should use Object.getPrototypeOf() for better practice) that points to its prototype. The prototype property is exclusive to functions, and it's what gets assigned as the __proto__ of any instances created with that function via new.
内容的提问来源于stack exchange,提问作者Kushagra Sinha

