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
不同,但我不明白
。我已经尝试搜索线程,但仍然无法回答自己。抱歉,如果这与任何内容重复。
Given these code
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
Why does the last line console.log(test4.toString())
throws error ? It shows that test4.toString
is not like test5.toString
but I don't get it ..
Ps. I've tried searching the threads and still cannot answer myself. Sorry if this duplicates with any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而不是这样:
您应该这样做:
按照您的方式,
test4
在其原型链中具有Person
function,而不是预期的原型具有toString
方法的对象。因此,它使用了一个
toString()
方法,该方法显然预计会针对Function
对象进行调用。Instead of this:
You should be doing:
The way you had it,
test4
had thePerson
function in its prototype chain, not the intended prototype object that has yourtoString
method.Because of this, it was using a
toString()
method that is apparently anticipating being called against aFunction
object.分配给原型和将新属性分配给对象的原型是有区别的。
您将函数 Person 声明为构造函数,但是您实际上是通过这样做向其原型分配了一些内容:
这意味着您将一个新的对象键值对 toString-function 分配给 Person.prototype,而不是实际添加一个新属性对此,你应该这样做:
这意味着当你实际上通过调用
Object.create
创建一个继承自 Person 对象的新对象时,那么它的实现中会发生什么是一个新对象将被重新创建,并且然后它将返回该新对象,该对象将覆盖 javascript 假定您通过之前在代码中执行Person.prototype
赋值创建的原型属性。There is a difference between assigning to a prototype and assigning a new property to the prototype of an object.
You declared a function Person as a constructor function, but then you are pretty much assigning something to its prototype by doing this:
That means you assigning a new object key value pair toString-function to Person.prototype, instead of actually adding a new property to it, in which you should have done like this:
What entails from this is that when you are actually creating a new object that inherits from Person object by calling
Object.create
, then what happens in its implementation is a new object will be freshly created, and then it will return that new object which will override the prototype property that javascript assumed you have created by doing thatPerson.prototype
assignment previously earlier in your code.p1 是一个对象
p2 是一个函数
p1 is an Object
p2 is a function