[英]What type of object is a Javascript prototype?
function Person(name) {
this.name = name;
}
var rob = new Person('Rob');
__proto__
) is Person.prototype, so rob is a Person. rob的原型( __proto__
)是Person.prototype,因此rob是一个Person。 But 但
console.log(Person.prototype);
outputs 输出
Person {}
Is Person.prototype an object? Person.prototype是一个对象吗? Array? 阵列? A Person? 一个人?
If it is an Object, does that prototype also have a prototype? 如果它是一个Object,该原型是否也有原型?
Update on what I have learned from this question (Friday 24 January 2014, 11:38:26 AM) 更新我从这个问题中学到的知识(2014年1月24日,星期五,上午11:38:26)
function Person(name) {
this.name = name;
}
var rob = new Person('Rob');
// Person.prototype references the object that will be the actual prototype (x.__proto__)
// for any object created using "x = new Person()". The same goes for Object. This is what
// Person and Object's prototype looks like.
console.log(Person.prototype); // Person {}
console.log(Object.prototype); // Object {}
console.log(rob.__proto__); // Person {}
console.log(rob.__proto__.__proto__); // Object {}
console.log(typeof rob); // object
console.log(rob instanceof Person); // true, because rob.__proto__ == Person.prototype
console.log(rob instanceof Object); // true, because rob.__proto__.__proto__ == Object.prototype
console.log(typeof rob.__proto__); // object
console.log(rob.__proto__ instanceof Person); // false
console.log(rob.__proto__ instanceof Object); // true, because rob.__proto__.__proto__ == Object.prototype
Is Person.prototype an object? Person.prototype是一个对象吗?
Yes. 是。 Everything (interesting) is an object :-) See How is almost everything in Javascript an object? 一切(有趣的)都是一个对象:-)看看Javascript中的几乎所有内容都是一个对象? for details. 详情。
An Array? 数组?
No, definitively not. 不,绝对不是。
A Person? 一个人?
Depends. 要看。 Most people would say it's not an instance of Person
- it's the essence of all Persons (their prototype :-). 大多数人会说这是不是一个实例Person
-这是所有的人(他们的原型:-)的精髓。 However, console.log
seems to identify it as such because it has a .constructor
property pointing to the Person
constructor. 但是, console.log
似乎将其识别为,因为它具有指向Person
构造函数的.constructor
属性。
If it is an Object, does that prototype also have a prototype? 如果它是一个Object,该原型是否也有原型?
Yes. 是。 Every Object does have a prototype. 每个对象都有一个原型。 These build the so-called prototype chain , at whose end is a null
reference. 这些构建了所谓的原型链 ,其末端是null
引用。 In your particular example, it's 在你的特定例子中,它是
rob
|
v
Person.prototype
|
v
Object.prototype
|
v
null
typeof Person === "function"
typeof Person.prototype === "object"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.