Object.prototype.toLocaleString()

Object 实例的 toLocaleString() 方法返回表示该对象的字符串。此方法旨在被派生对象覆盖以用于特定于区域设置的目的。

¥The toLocaleString() method of Object instances returns a string representing this object. This method is meant to be overridden by derived objects for locale-specific purposes.

Try it

语法

¥Syntax

js
toLocaleString()

参数

¥Parameters

没有任何。但是,所有重写此方法的对象都应最多接受两个参数,对应于 localesoptions,例如 Date.prototype.toLocaleString。参数位置不应用于任何其他目的。

¥None. However, all objects that override this method are expected to accept at most two parameters, corresponding to locales and options, such as Date.prototype.toLocaleString. The parameter positions should not be used for any other purpose.

返回值

¥Return value

调用 this.toString() 的返回值。

¥The return value of calling this.toString().

描述

¥Description

所有从 Object.prototype 继承的对象(即除 null-原型对象 之外的所有对象)都继承 toLocaleString() 方法。ObjecttoLocaleString 返回调用 this.toString() 的结果。

¥All objects that inherit from Object.prototype (that is, all except null-prototype objects) inherit the toLocaleString() method. Object's toLocaleString returns the result of calling this.toString().

提供此函数是为了给对象提供通用的 toLocaleString 方法,尽管并非所有对象都可以使用它。在核心语言中,这些内置对象覆盖 toLocaleString 以提供特定于区域设置的格式:

¥This function is provided to give objects a generic toLocaleString method, even though not all may use it. In the core language, these built-in objects override toLocaleString to provide locale-specific formatting:

示例

¥Examples

使用基本的 toLocaleString() 方法

¥Using the base toLocaleString() method

基本 toLocaleString() 方法仅调用 toString()

¥The base toLocaleString() method simply calls toString().

js
const obj = {
  toString() {
    return "My Object";
  },
};
console.log(obj.toLocaleString()); // "My Object"

数组 toLocaleString() 覆盖

¥Array toLocaleString() override

Array.prototype.toLocaleString() 用于通过调用每个元素的 toLocaleString() 方法并将结果与特定于区域设置的分隔符连接起来,将数组值打印为字符串。例如:

¥Array.prototype.toLocaleString() is used to print array values as a string by invoking each element's toLocaleString() method and joining the results with a locale-specific separator. For example:

js
const testArray = [4, 7, 10];

const euroPrices = testArray.toLocaleString("fr", {
  style: "currency",
  currency: "EUR",
});
// "4,00 €,7,00 €,10,00 €"

日期 toLocaleString() 覆盖

¥Date toLocaleString() override

Date.prototype.toLocaleString() 用于打印出更适合特定区域设置的日期显示。例如:

¥Date.prototype.toLocaleString() is used to print out date displays more suitable for specific locales. For example:

js
const testDate = new Date();
// "Fri May 29 2020 18:04:24 GMT+0100 (British Summer Time)"

const deDate = testDate.toLocaleString("de");
// "29.5.2020, 18:04:24"

const frDate = testDate.toLocaleString("fr");
// "29/05/2020, 18:04:24"

数字 toLocaleString() 覆盖

¥Number toLocaleString() override

Number.prototype.toLocaleString() 用于打印出更适合特定区域设置的数字显示,例如 使用正确的分隔符。例如:

¥Number.prototype.toLocaleString() is used to print out number displays more suitable for specific locales, e.g. with the correct separators. For example:

js
const testNumber = 2901234564;
// "2901234564"

const deNumber = testNumber.toLocaleString("de");
// "2.901.234.564"

const frNumber = testNumber.toLocaleString("fr");
// "2 901 234 564"

规范

Specification
ECMAScript Language Specification
# sec-object.prototype.tolocalestring

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看