简体   繁体   English

什么类型的对象是Javascript原型?

[英]What type of object is a Javascript prototype?

function Person(name) {
  this.name = name;
}
var rob = new Person('Rob');
  • Person is a function. 人是一种功能。
  • Person is an object. 人是一个对象。
  • Person is not a Person. 人不是人。
  • rob is an object. 抢是一个对象。
  • rob's prototype ( __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
  • Prototypes are just plain objects. 原型只是普通的对象。
  • typeof is useful to determine if something is object or primitive (and what sort of primitive it is), but useless to determine what sort of object it is. typeof可用于确定某些东西是对象还是原始的(以及它是什么类型的原语),但无法确定它是什么类型的对象。
  • LHS instanceof RHS returns true if RHS.prototype turns up somewhere in the prototype chain of LHS. 如果RHS.prototype在LHS原型链中的某处出现,则RHS实例的RHS返回true。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM