javascript - Is Object.prototype an object and is prototype somehow not? - Stack Overflow

When I type Object.prototype into the Chrome console it displays Object{}If I assign it to a variable v

When I type Object.prototype into the Chrome console it displays Object{}

If I assign it to a variable var proto = Object.prototype it displays Object{}

But if I check for it's prototype

var proto = Object.prototype
proto.prototype;

If proto is an Object{} and proto.prototype is undefined does that means thatObject() is an Object() that doesn't inherit from anything?

Also, would this be the same as Object.prototype.prototype?

When I type Object.prototype into the Chrome console it displays Object{}

If I assign it to a variable var proto = Object.prototype it displays Object{}

But if I check for it's prototype

var proto = Object.prototype
proto.prototype;

If proto is an Object{} and proto.prototype is undefined does that means thatObject() is an Object() that doesn't inherit from anything?

Also, would this be the same as Object.prototype.prototype?

Share Improve this question asked Apr 25, 2015 at 15:53 jeanpier_rejeanpier_re 8351 gold badge6 silver badges23 bronze badges 5
  • It is an object, and it has no prototype as it is one. – adeneo Commented Apr 25, 2015 at 15:57
  • So is prototype kind of like Object.create(null)? – jeanpier_re Commented Apr 25, 2015 at 15:59
  • 1 When it es to inheritance, JavaScript only has one construct: objects. Each object has an internal link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. null, by definition, has no prototype, and acts as the final link in this prototype chain. – adeneo Commented Apr 25, 2015 at 15:59
  • 1 Why does it return undefined and not null? – jeanpier_re Commented Apr 25, 2015 at 16:06
  • Because it's not defined, the prototype chain has ended – adeneo Commented Apr 25, 2015 at 16:07
Add a ment  | 

3 Answers 3

Reset to default 5

If an Object has a prototype, the prototype is also an Object. These Objects all chain together until you reach null, which is the end of the prototype chain.

// 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

You should also note, however, that you can't keep accessing the obj.prototype property, as this is only applicable to Constructors, consider

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

The correct way to find the prototype of an Object is by using Object.getPrototypeOf(obj),

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

It may also be of note that legacy browsers may not support Object.getPrototypeOf, in which case many offer the property obj.__proto__. However, try to avoid using __proto__ outside of a shim for such browsers if you need to access the prototype chain.


Finally, using new with a Constructor isn't the only way to create this chain, you can set them up using Object.create

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

Also consider

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

Object.prototype is the furthest you can go up the prototype chain.

It Object.prototype has no prototype property, so what you see is undefined. In actuality, it's prototype is null. Which you can confirm with either the Object.getPrototypeOf method or the __proto__ property.

 Object.getPrototypeOf(Object.prototype); // null
 Object.prototype.__proto__; // null

The prototype property isn't a reliable way to traverse the prototype chain, as in many cases, it won't exist.

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

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

The __proto__ property is not a standard and may be removed from a future edition of ES. But in patible browsers, it will work in the expected way.

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

A lot can be said about prototypes, but the brief explanations below could help clarify the subject to make it easier to understand:

A) prototype is a property of functions.

Object.constructor returns function Function() { [native code] }, so we can get its prototype, just like how you were able to get the prototype of Object itself. This is because it is a function. typeof Object returns "function". On the other hand, typeof Object.prototype returns "object", so you were unable to get the prototype of 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

We can add properties and methods to a Constructor (which is a function) using the prototype property.

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__ is a property of all objects (which includes functions).

Note that Object.__proto__ is equivalent to Object.constructor.prototype and Object.getPrototype(Object).

There is no prototype property of Object.prototype because it is not a function. But because it is an object, we can do Object.prototype.__proto__ which returns null. Getting null means we are at the end of the prototype chain.

Even functions have __proto__:

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

__proto__ is the internal prototype that is used by JS engines for inheritance/delegate chaining.

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745381060a4625227.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信