且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 js toString() 方法转换对象会给出奇怪的结果?

更新时间:2023-01-28 16:22:16

在 Javascript 中,所有对象都继承自 Object.对于自定义对象,如果您没有定义 toString() 方法,它将从其父类继承它.因此 obj.toString() 打印[object Object]",因为它是 Object 类型的对象(原始类型).

In Javascript all objects inherit from Object. For a custom object if you don't define the toString() method it will inherit it from its parent class. So obj.toString() prints "[object Object]" because it is an object (a primitive type) of type Object.