Intl.RelativeTimeFormat.prototype.resolvedOptions()
resolvedOptions() 方法是 Intl.RelativeTimeFormat 实例上的一个方法,它返回一个新的对象,该对象的属性反映了在此 RelativeTimeFormat 对象初始化期间计算的选项。
试一试
const rtf1 = new Intl.RelativeTimeFormat("en", { style: "narrow" });
const options1 = rtf1.resolvedOptions();
const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
const options2 = rtf2.resolvedOptions();
console.log(`${options1.locale}, ${options1.style}, ${options1.numeric}`);
// Expected output: "en, narrow, always"
console.log(`${options2.locale}, ${options2.style}, ${options2.numeric}`);
// Expected output: "es, long, auto"
语法
js
resolvedOptions()
参数
无。
返回值
一个新的对象,其属性反映了在此 RelativeTimeFormat 对象初始化期间计算的选项。该对象具有以下属性,按照列出的顺序排列
locale-
通过 区域设置协商 过程确定的实际使用的区域设置的 BCP 47 语言标签。输出中可能只包含请求的
nuUnicode 扩展键(如果已请求)。 style-
在
options参数中为此属性提供的值,根据需要填充了默认值。它可以是"long"、"short"或"narrow"。默认值为"long"。 numeric-
在
options参数中为此属性提供的值,根据需要填充了默认值。它可以是"always"或"auto"。默认值为"always"。 numberingSystem-
在
options参数中为该属性提供的值,或使用 Unicode 扩展键"nu",并根据需要填充默认值。它是此区域设置支持的 数字系统。默认值取决于区域设置。
示例
使用 resolvedOptions() 方法
js
const de = new Intl.RelativeTimeFormat("de-DE");
const usedOptions = de.resolvedOptions();
usedOptions.locale; // "de-DE"
usedOptions.style; // "long"
usedOptions.numeric; // "always"
usedOptions.numberingSystem; // "latn"
规范
| 规范 |
|---|
| ECMAScript® 2026 国际化 API 规范 # sec-intl.relativetimeformat.prototype.resolvedoptions |
浏览器兼容性
加载中…