Temporal.PlainYearMonth.prototype.equals()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

equals() 方法用於 Temporal.PlainYearMonth 例項,當此年月與其另一個年月在值上等效時(可由 Temporal.PlainYearMonth.from() 轉換的形式),返回 true,否則返回 false。它們會同時根據其底層的 ISO 日期值和日曆進行比較,因此來自不同日曆的兩個年月可能在 Temporal.PlainYearMonth.compare() 中被視為相等,但在 equals() 中不被視為相等。

注意: PlainYearMonth 物件會跟蹤一個參考 ISO 日期,該日期也用於比較。當使用 Temporal.PlainYearMonth.from() 方法時,該日期會自動設定;但可以使用 Temporal.PlainYearMonth() 建構函式手動設定,這會導致兩個等效的年月因參考日期不同而被視為不同。因此,您應該避免直接使用建構函式,而優先使用 from() 方法。

語法

js
equals(other)

引數

其他

要比較的另一個年月的字串、物件或 Temporal.PlainYearMonth 例項。它使用與 Temporal.PlainYearMonth.from() 相同的演算法轉換為 Temporal.PlainYearMonth 物件。

返回值

如果此年月在日期值和日曆上都等於 other,則返回 true,否則返回 false

示例

使用 equals()

js
const ym1 = Temporal.PlainYearMonth.from("2021-08");
const ym2 = Temporal.PlainYearMonth.from({ year: 2021, month: 8 });
console.log(ym1.equals(ym2)); // true

const ym3 = Temporal.PlainYearMonth.from("2021-08-01[u-ca=japanese]");
console.log(ym1.equals(ym3)); // false

const ym4 = Temporal.PlainYearMonth.from("2021-09");
console.log(ym1.equals(ym4)); // false

規範

規範
Temporal
# sec-temporal.plainyearmonth.prototype.equals

瀏覽器相容性

另見