Object.create() 和 toString()

发布于 2024-12-21 20:48:30 字数 1009 浏览 1 评论 0原文

鉴于这些代码

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.toStringtest5.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2024-12-28 20:48:30

而不是这样:

var test4 = Object.create(Person);

您应该这样做:

var test4 = Object.create(Person.prototype);

按照您的方式,test4在其原型链中具有Personfunction,而不是预期的原型具有 toString 方法的对象。

因此,它使用了一个 toString() 方法,该方法显然预计会针对 Function 对象进行调用。

Instead of this:

var test4 = Object.create(Person);

You should be doing:

var test4 = Object.create(Person.prototype);

The way you had it, test4 had the Person function in its prototype chain, not the intended prototype object that has your toString method.

Because of this, it was using a toString() method that is apparently anticipating being called against a Function object.

橘香 2024-12-28 20:48:30

分配给原型和将新属性分配给对象的原型是有区别的。

您将函数 Person 声明为构造函数,但是您实际上是通过这样做向其原型分配了一些内容:

Person.prototype = {
  toString: function() { return this.firstName + ' ' + this.lastName; }
};

这意味着您将一个新的对象键值对 toString-function 分配给 Person.prototype,而不是实际添加一个新属性对此,你应该这样做:

Person.prototype.toString = function() { return this.firstName + ' ' + this.lastName; }

这意味着当你实际上通过调用 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:

Person.prototype = {
  toString: function() { return this.firstName + ' ' + this.lastName; }
};

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:

Person.prototype.toString = function() { return this.firstName + ' ' + this.lastName; }

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 that Person.prototype assignment previously earlier in your code.

梦在夏天 2024-12-28 20:48:30
var Person = function(firstName, lastName) { 
    this.firstName = firstName; 
    this.lastName = lastName;
  }; var p1=new Person("Phat1","Wang1");

p1 是一个对象

var p2= Object.create(Person);
p2.firstName="Phat2";
p2.lastName="Wang2";

p2 是一个函数

var Person = function(firstName, lastName) { 
    this.firstName = firstName; 
    this.lastName = lastName;
  }; var p1=new Person("Phat1","Wang1");

p1 is an Object

var p2= Object.create(Person);
p2.firstName="Phat2";
p2.lastName="Wang2";

p2 is a function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文