--- title: Object.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/Object/toString tags: - JavaScript - Method - Object - Prototype - 原型 - 对象 - 方法 translation_of: Web/JavaScript/Reference/Global_Objects/Object/toString ---
{{JSRef}}
toString() 方法返回一个表示该对象的字符串。
obj.toString()
一个表示该对象的字符串。
每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString() 方法被每个 Object 对象继承。如果此方法在自定义对象中未被覆盖,toString() 返回 "[object type]",其中 type 是对象的类型。以下代码说明了这一点:
var o = new Object(); o.toString(); // returns [object Object]
toString() 调用 {{jsxref("null")}} 返回[object Null],{{jsxref("undefined")}} 返回 [object Undefined]。请参阅下面的{{anch("Using_toString()_to_detect_object_class", "使用 toString() 检测对象类型")}}。toString 方法可以自定义一个方法,来取代默认的 toString() 方法。该 toString() 方法不能传入参数,并且必须返回一个字符串。自定义的 toString() 方法可以是任何我们需要的值,但如果它附带有关对象的信息,它将变得非常有用。
以下代码定义了 Dog 对象类型,并创建了一个 Dog 类型的 theDog 对象:
function Dog(name,breed,color,sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}
var theDog = new Dog("Gabby", "Lab", "chocolate", "female");
如果当前的对象调用了 toString() 方法,它将会返回从 {{jsxref("Object")}}继承而来的 toString() 方法的返回默认值:
theDog.toString(); // 返回 [object Object]
下面的代码中定义了一个叫做 dogToString() 的方法来覆盖默认的 toString() 方法。这个方法生成一个 "property = value;" 形式的字符串,该字符串包含了当前对象的 name、breed、color 和 sex 的值。
Dog.prototype.toString = function dogToString() {
 var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed;
 return ret;
}
也可以这样写
Dog.prototype.toString = function dogToString() {
  return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
}
使用上述代码,任何时候在字符串上下文中使用 theDog.toString() 时,JavaScript 都会自动调用 dogToString() 方法(dogToString() 可以是一个匿名函数),并且返回以下字符串:
"Dog Gabby is a female chocolate Lab"
toString() 检测对象类型可以通过 toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg。
var toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] //Since JavaScript 1.8.5 toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
| 规范 | 状态 | 备注 | 
|---|---|---|
| {{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.0. | 
| {{SpecName('ES5.1', '#sec-15.2.4.2', 'Object.prototype.toString')}} | {{Spec2('ES5.1')}} | Call on {{jsxref("null")}} returns [object Null], and {{jsxref("undefined")}} returns [object Undefined] | 
  
| {{SpecName('ES6', '#sec-object.prototype.tostring', 'Object.prototype.toString')}} | {{Spec2('ES6')}} | |
| {{SpecName('ESDraft', '#sec-object.prototype.tostring', 'Object.prototype.toString')}} | {{Spec2('ESDraft')}} | 
{{Compat("javascript.builtins.Object.toString")}}