[英]JavaScript protyping - differences
Here's my sample code 这是我的示例代码
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
constructor: Person,
printInformation: function() {
console.log(this.toString());
},
toString: function() {
return "Name: " + this.name + ", Age: " + this.age;
}
};
var person1 = new Person("Some Name", 15);
person1.printInformation();
console.log(typeof(person1));
console.log(Object.getPrototypeOf(person1) === Object.prototype);
var book = {
title: "Some book",
author: "Some author",
printInformation: function() {
console.log(this.toString());
},
toString: function() {
return "Book: " + this.title + ", Author(s): " + this.author;
}
};
book.printInformation();
var bookPrototype = Object.getPrototypeOf(book);
console.log(typeof(book));
console.log(Object.getPrototypeOf(book) === Object.prototype);
Output: 输出:
Name: Some Name, Age: 15
object
false
Book: Some book, Author(s): Some author
object
true
Why does Object.getPrototypeOf(person1) === Object.prototype
return false while Object.getPrototypeOf(book) === Object.prototype
return true? 为什么
Object.getPrototypeOf(person1) === Object.prototype
返回false,而Object.getPrototypeOf(book) === Object.prototype
返回true?
Both are instances of object, both point to a prototype, I'd hope, both should return true. 两者都是对象的实例,都指向原型,我希望它们都应返回true。 Kindly enlighten me.
请赐教。
The prototype chain of person1
looks like this: person1
的原型链如下所示:
person1 ---> Person.prototype ---> Object.prototype ---> null
The prototype chain of book
looks like this: book
的原型链如下所示:
book ---> Object.prototype ---> null
The Object.getPrototypeOf()
method returns the next item in the prototype chain. Object.getPrototypeOf()
方法返回原型链中的下一项。 Therefore, person1
does not return Object.prototype
and is therefore false
. 因此,
person1
不返回Object.prototype
,因此为false
。
To get the person1
to give true
, you'd have to cycle calls until you reached Object.prototype
. 为了使
person1
成为true
,您必须循环调用直到到达Object.prototype
。
var obj = person1
while (obj) {
if (obj === Object.prototype) {
console.log("found it!");
break;
}
obj = Object.getPrototypeOf(obj);
}
Or if the prototype object is indeed on a function, you could just use instanceof
instead. 或者,如果原型对象确实在函数上,则可以使用
instanceof
代替。
person1 instanceof Object; // true
book instanceof Object; // true
The instanceof
searches the prototype chain of the object you provide to see if it has any object that matches the .prototype
of the function you provide, which in this case is the Object
function. instanceof
搜索您提供的对象的原型链,以查看其是否具有与您提供的函数的.prototype
相匹配的对象,在本例中为Object
函数。
With your Person
prototype, you're explicitly defining as a 'Person' type object, where as with your book
, it is just a generic object that happens to have a variable name of book
. 对于您的
Person
原型,您将显式定义为“ Person”类型的对象,与您的book
,它只是一个碰巧具有book
变量名称的通用对象。
Object.getPrototypeOf(book)
Console outputs Object {}
控制台输出
Object {}
Object.getPrototypeOf(person1)
Console outputs Person {}
控制台输出
Person {}
Person
is not the same as Object
and thus the check for equality returns false. Person
与Object
,因此相等性检查返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.