概述

toString() 方法返回指定对象的字符串形式。

语法

str.toString()

描述

String 对象覆盖了Object 对象的 toString 方法;并没有继承 Object.toString()。对于 String 对象,toString 方法返回该对象的字符串形式,和 String.prototype.valueOf() 方法返回值一样。

示例

例子:使用 toString

下例输出一个字符串对象(String object)的字符串值:

var x = new String("Hello world");

alert(x.toString())      // 输出 "Hello world"

规范

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
Implemented in JavaScript 1.1.
ECMAScript 5.1 (ECMA-262)
String.prototype.toString
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
String.prototype.toString
Standard  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

相关链接