Array.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Array 实例的 toString() 方法返回表示指定数组及其元素的字符串。

¥The toString() method of Array instances returns a string representing the specified array and its elements.

Try it

语法

¥Syntax

js
toString()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

表示数组元素的字符串。

¥A string representing the elements of the array.

描述

¥Description

Array 对象覆盖 ObjecttoString 方法。数组的 toString 方法在内部调用 join(),该方法连接数组并返回一个字符串,其中包含以逗号分隔的每个数组元素。如果 join 方法不可用或者不是函数,则使用 Object.prototype.toString 代替,返回 [object Array]

¥The Array object overrides the toString method of Object. The toString method of arrays calls join() internally, which joins the array and returns one string containing each array element separated by commas. If the join method is unavailable or is not a function, Object.prototype.toString is used instead, returning [object Array].

js
const arr = [];
arr.join = 1; // re-assign `join` with a non-function
console.log(arr.toString()); // [object Array]

console.log(Array.prototype.toString.call({ join: () => 1 })); // 1

当要将数组表示为文本值或在字符串连接中引用数组时,JavaScript 会自动调用 toString 方法。

¥JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.

Array.prototype.toString 递归地将每个元素(包括其他数组)转换为字符串。由于 Array.prototype.toString 返回的字符串没有分隔符,因此嵌套数组看起来像是被展平的。

¥Array.prototype.toString recursively converts each element, including other arrays, to strings. Because the string returned by Array.prototype.toString does not have delimiters, nested arrays look like they are flattened.

js
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(matrix.toString()); // 1,2,3,4,5,6,7,8,9

当数组是循环的(它包含本身的元素)时,浏览器通过忽略循环引用来避免无限递归。

¥When an array is cyclic (it contains an element that is itself), browsers avoid infinite recursion by ignoring the cyclic reference.

js
const arr = [];
arr.push(1, [3, arr, 4], 2);
console.log(arr.toString()); // 1,3,,4,2

示例

¥Examples

使用 toString()

¥Using toString()

js
const array1 = [1, 2, "a", "1a"];

console.log(array1.toString()); // "1,2,a,1a"

在稀疏数组上使用 toString()

¥Using toString() on sparse arrays

按照 join() 的行为,toString()undefined 一样对待空槽并生成一个额外的分隔符:

¥Following the behavior of join(), toString() treats empty slots the same as undefined and produces an extra separator:

js
console.log([1, , 3].toString()); // '1,,3'

对非数组对象调用 toString()

¥Calling toString() on non-array objects

toString()generic。它期望 this 有一个 join() 方法;或者,如果失败,则使用 Object.prototype.toString() 代替。

¥toString() is generic. It expects this to have a join() method; or, failing that, uses Object.prototype.toString() instead.

js
console.log(Array.prototype.toString.call({ join: () => 1 }));
// 1; a number
console.log(Array.prototype.toString.call({ join: () => undefined }));
// undefined
console.log(Array.prototype.toString.call({ join: "not function" }));
// "[object Object]"

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.tostring

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看