Intl.ListFormat

Baseline 已廣泛支援

此特性已得到良好支援,可在多種裝置和瀏覽器版本上使用。自 2021 年 4 月起,所有瀏覽器均已支援此特性。

Intl.ListFormat 物件可用於生成符合語言習慣的列表格式化字串。

試一試

const vehicles = ["Motorcycle", "Bus", "Car"];

const formatter = new Intl.ListFormat("en", {
  style: "long",
  type: "conjunction",
});
console.log(formatter.format(vehicles));
// Expected output: "Motorcycle, Bus, and Car"

const formatter2 = new Intl.ListFormat("de", {
  style: "short",
  type: "disjunction",
});
console.log(formatter2.format(vehicles));
// Expected output: "Motorcycle, Bus oder Car"

const formatter3 = new Intl.ListFormat("en", { style: "narrow", type: "unit" });
console.log(formatter3.format(vehicles));
// Expected output: "Motorcycle Bus Car"

建構函式

Intl.ListFormat()

建立一個新的 Intl.ListFormat 物件。

靜態方法

Intl.ListFormat.supportedLocalesOf()

返回一個數組,其中包含提供的區域設定中受支援的那些區域設定,而無需回退到執行時預設區域設定。

例項屬性

這些屬性定義在 Intl.ListFormat.prototype 上,並由所有 Intl.ListFormat 例項共享。

Intl.ListFormat.prototype.constructor

建立例項物件的建構函式。對於 Intl.ListFormat 例項,初始值為 Intl.ListFormat 建構函式。

Intl.ListFormat.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 屬性的初始值為字串 "Intl.ListFormat"。此屬性用於 Object.prototype.toString()

例項方法

Intl.ListFormat.prototype.format()

返回一個符合語言習慣的格式化字串,表示列表中的元素。

Intl.ListFormat.prototype.formatToParts()

返回一個物件陣列,這些物件表示可以用來以符合區域設定的方式格式化值列表的各個元件。

Intl.ListFormat.prototype.resolvedOptions()

返回一個新物件,其中包含在當前 Intl.ListFormat 物件構造期間計算出的區域設定和樣式格式化選項的屬性。

示例

使用格式

以下示例展示瞭如何使用英語建立列表格式化程式。

js
const list = ["Motorcycle", "Bus", "Car"];

console.log(
  new Intl.ListFormat("en-GB", { style: "long", type: "conjunction" }).format(
    list,
  ),
);
// Motorcycle, Bus and Car

console.log(
  new Intl.ListFormat("en-GB", { style: "short", type: "disjunction" }).format(
    list,
  ),
);
// Motorcycle, Bus or Car

console.log(
  new Intl.ListFormat("en-GB", { style: "narrow", type: "unit" }).format(list),
);
// Motorcycle Bus Car

使用 formatToParts

以下示例顯示瞭如何建立返回格式化部分的 List formatter

js
const list = ["Motorcycle", "Bus", "Car"];
console.log(
  new Intl.ListFormat("en-GB", {
    style: "long",
    type: "conjunction",
  }).formatToParts(list),
);

// [ { "type": "element", "value": "Motorcycle" },
//   { "type": "literal", "value": ", " },
//   { "type": "element", "value": "Bus" },
//   { "type": "literal", "value": ", and " },
//   { "type": "element", "value": "Car" } ];

規範

規範
ECMAScript® 2026 國際化 API 規範
# listformat-objects

瀏覽器相容性

另見