Object.prototype 是一个对象而原型不是吗?

Is Object.prototype an object and is prototype somehow not?

当我在 Chrome 控制台中输入 Object.prototype 时,它会显示 Object{}

如果我将它分配给一个变量 var proto = Object.prototype 它会显示 Object{}

但如果我检查它的原型

var proto = Object.prototype
proto.prototype;

如果 proto 是一个 Object{}proto.prototype 是未定义的,这是否意味着 Object() 一个 Object() 不继承任何东西?

此外,这是否与 Object.prototype.prototype 相同?

如果一个对象有一个原型,那么这个原型也是一个对象。这些对象全部链接在一起,直到到达null,这是原型链的末端。

// Object is a constructor
Object.prototype; // Object {}, will be the prototype of `new Object`s
// Object.prototype is an Object
Object.getPrototypeOf(Object.prototype); // null, we are at the end of the chain

但是您还应该注意,您不能继续访问 obj.prototype 属性,因为这仅适用于 Constructors,请考虑

function Foo() {
}
Foo.prototype; // Foo {}
// vs
(new Foo).prototype; // undefined

找到 Object 原型的正确方法是使用 Object.getPrototypeOf(obj)

Object.getPrototypeOf(new Foo) === Foo.prototype; // true

可能还需要注意的是,旧版浏览器可能不支持 Object.getPrototypeOf,在这种情况下,许多浏览器提供 属性 obj.__proto__。但是,如果您需要访问原型链,请尽量避免在此类浏览器的 shim 之外使用 __proto__


最后,使用 newConstructor 并不是创建此链的唯一方法,您可以使用 Object.create[= 设置它们22=]

var a = Object.create(null),
    b = Object.create(a), // b will inherit from a
    c = Object.create(b); // c will inherit from b, hence also a
a.foo = 'foo';
b.bar = 'bar';

a instanceof Object; // false

a.bar; // undefined
c.foo + c.bar === 'foobar'; // true

也考虑

c.prototype; // undefined
// vs
Object.getPrototypeOf(c) === b; // true

Object.prototype 是原型链上最远的地方。

Object.prototype没有原型属性,所以你看到的是undefined。其实它的原型是null。您可以使用 Object.getPrototypeOf 方法或 __proto__ 属性.

方法确认
 Object.getPrototypeOf(Object.prototype); // null
 Object.prototype.__proto__; // null

prototype 属性 不是遍历原型链的可靠方法,因为在许多情况下,它不存在。

function MyObj() {}
MyObj.prototype.a = 3;
(new MyObj).prototype; // undefined

Object.create({ a: 3 }).prototype; // undefined

__proto__ property 不是标准,可能会从未来的 ES 版本中删除。但在兼容的浏览器中,它将以预期的方式工作。

Object.create({ a: 3 }).__proto__; // Object {a: 3}

关于原型可以说很多,但下面的简短解释有助于澄清主题,使其更容易理解:

A) prototype 是 属性 函数 .

Object.constructor returns function Function() { [native code] },所以我们可以得到它的 prototype,就像你如何能够得到 [=19] 的 prototype =] 本身。这是因为它是一个函数。 typeof Object returns "function"。另一方面,typeof Object.prototype returns "object",所以你无法得到Object.prototype的原型。

// get prototype property of a function
var hello = function () {};
hello.prototype // hello {}

// get prototype property of an object
typeof hello.prototype // "object"
hello.prototype.prototype // undefined
hello = {};
hello.prototype // undefined

我们可以使用 prototype 属性.

将属性和方法添加到 Constructor(这是一个函数)
function Hello() {}
Hello.prototype.print = function () { console.log('hello'); };

// not using a constructor
var hello = Hello(); // undefined
hello.print(); // ERROR

// using a constructor
hello = new Hello(); // Hello {print: function}
hello.print(); // "hello"

B) __proto__ 是所有对象(包括函数)的 属性.

请注意 Object.__proto__ 等同于 Object.constructor.prototypeObject.getPrototype(Object)

没有Object.prototypeprototype属性,因为它不是函数。但是因为它是一个对象,我们可以做到Object.prototype.__proto__ which returns null。得到 null 意味着我们在原型链的末端。

连函数都有__proto__:

function Hello() {}
Hello.__proto__ // function Empty() {}

__proto__ 是 JS 引擎用于 inheritance/delegate 链接的内部原型。

function Hello() {}
var hello = new Hello();

hello.__proto__ === Hello.prototype // true