【JavaScript ECMAScript】Temporal::Temporal.PlainYearMonth.prototype.yearインスタンスアクセサプロパティの使い方
Temporal.PlainYearMonth.prototype.yearインスタンスアクセサプロパティの使い方について、初心者にもわかりやすく解説します。
基本的な使い方
Temporal.PlainYearMonth.prototype.yearプロパティは、JavaScriptの新しい日付時刻APIであるTemporalに含まれるPlainYearMonthオブジェクトが保持する「年」の値を参照するためのプロパティです。PlainYearMonthオブジェクトは、特定の日や時間、タイムゾーン情報を持たず、年と月のみを表現するシンプルな日付オブジェクトとして設計されています。
このyearプロパティは、既存のPlainYearMonthインスタンスから、そのオブジェクトが表す年を数値として取得するために使用されます。例えば、2023年10月を表すPlainYearMonthオブジェクトを作成した場合、yearプロパティにアクセスすることで「2023」という数値を読み取ることができます。
このプロパティは「アクセサープロパティ」であり、値を読み取る専用です。一度PlainYearMonthオブジェクトが作成されると、そのyearプロパティの値を直接変更することはできません。年を変更したい場合は、新しいPlainYearMonthオブジェクトを作成する必要があります。
Temporal.PlainYearMonth.prototype.yearプロパティは、カレンダー機能や会計システムなど、日付の中から年情報だけを効率的かつ明確に抽出したい場合に非常に役立ちます。システムエンジニアとして日付や時刻を扱う場面は多いため、このような特定の情報にアクセスするプロパティの理解は、正確なデータ処理を行う上で重要です。
構文(syntax)
1const plainYearMonth = new Temporal.PlainYearMonth(2023, 10); 2const year = plainYearMonth.year;
引数(parameters)
引数なし
引数はありません
戻り値(return)
number
Temporal.PlainYearMonth オブジェクトが表す年の数値(1〜999999)を返します。