鑑於這些代碼的Object.create()和toString()
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
,但我不明白它..
Ps。我試過尋找線索,仍然無法回答自己。對不起,如果這與任何重複。
的node.js?或在瀏覽器中? – chakrit