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 |
瀏覽器相容性
載入中…