【发布时间】:2011-12-19 04:32:24
【问题描述】:
鉴于这些代码
var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Person.prototype = {
toString: function() { return this.firstName + ' ' + this.lastName; }
};
var test4 = Object.create(Person);
test4.firstName = "Phat4";
test4.lastName = "Wang4";
console.log(test4.toString === Object.toString); // true
console.log(test4.toString === Function.toString); // true
var test5 = { firstName: "Phat5", lastName: "Wang5" };
console.log(test5.toString === test4.toString); // false
console.log(test4.toString === Function.toString); // true
console.log(test5.toString === Object.prototype.toString); // true
console.log(test5.toString()); // [object Object]
console.log(test4.toString()); // Function.prototype.toString called on incompatible object
为什么最后一行console.log(test4.toString()) 会抛出错误?它表明test4.toString 不像test5.toString 但我不明白..
附言。我已经尝试搜索线程,但仍然无法回答自己。对不起,如果这与任何重复。
【问题讨论】:
标签: javascript inheritance object prototypal-inheritance