问题描述:

If I run this e.g. in jsFiddle:

function Animal() {

}

var animal = new Animal();

console.log(animal);

console.log(Animal.prototype);

console.log(animal.__proto__);

then I get these results (3x Animal {}) in the console of Chrome's DevTool window:

Animal {} (index):26

Animal {} (index):27

Animal {} (index):28

It is quite confusing in my opinion, because as we know only Animal.prototype and animal.__proto__ point in this case to the same object. Or there is a reasonable cause to do it so?

网友答案:

It is showing it as an object, and showing all of its attributes inside of it. In this case, since there are none, it is showing up blank. However, if we add an attribute to the method, as such

function Animal() {
    this.foo="bar";
}

var animal = new Animal();

console.log(animal);
console.log(Animal.prototype);
console.log(animal.__proto__);

we get:

Animal {foo: "bar"}
Animal {}
Animal {}

EDIT: Misread the question

from the MDN: When an object is created, its proto property is set to reference the same object as its internal [[Prototype]] (i.e. its constructor's prototype object). Assigning a new value to proto also changes the value of the internal [[Prototype]] property, except where the object is non–extensible.

相关阅读:
Top