Webエンジニア向けプログラミング解説動画をYouTubeで配信中!
▶ チャンネル登録はこちら

【JavaScript ECMAScript】Temporal::Temporal.PlainYearMonth.prototype.equalsインスタンスメソッドの使い方

Temporal.PlainYearMonth.prototype.equals()インスタンスメソッドの使い方について、初心者にもわかりやすく解説します。

作成日: 更新日:

基本的な使い方

Temporal.PlainYearMonth.prototype.equals()メソッドは、Temporal.PlainYearMonthインスタンスが持つ年と月が、別のTemporal.PlainYearMonthインスタンスのそれらと等しいかどうかを比較し、真偽値を返すメソッドです。

Temporal.PlainYearMonthオブジェクトは、カレンダーシステムにおける特定の年と月を表現するために設計されたものです。このequals()メソッドを使用することで、現在操作しているTemporal.PlainYearMonthオブジェクトが示す年と月が、引数として渡された別のTemporal.PlainYearMonthオブジェクトの年と月と、そしてカレンダーシステムを含めて完全に一致するかどうかを厳密に判定できます。

具体的には、このメソッドは比較対象となるTemporal.PlainYearMonthインスタンスを唯一の引数として受け取ります。比較の結果、両方のオブジェクトが同じ年、同じ月、そして同じカレンダーシステムを使用している場合にのみ、戻り値としてブール値のtrueを返します。もし、年や月の値が異なる場合、または年と月が同じであっても参照しているカレンダーシステムが異なる場合には、戻り値としてfalseを返します。

このメソッドは、例えば、特定の年月のデータがすでに存在するかどうかを確認したり、異なるデータソースから取得した年月の情報が同一のものであるかを検証したりするなど、日付や時間に関する比較ロジックを実装する際に非常に重要です。システム開発において、正確な期間の判定や条件分岐を行うために役立ちます。

構文(syntax)

1const plainYearMonth1 = new Temporal.PlainYearMonth(2023, 10);
2const plainYearMonth2 = new Temporal.PlainYearMonth(2023, 10);
3
4plainYearMonth1.equals(plainYearMonth2);

引数(parameters)

other

  • other: Temporal.PlainYearMonth: 比較対象となる別の Temporal.PlainYearMonth オブジェクト

戻り値(return)

boolean

このメソッドは、2つのPlainYearMonthオブジェクトが同じ年と月を表す場合にtrueを、そうでない場合にfalseを返します。

関連コンテンツ

関連プログラミング言語