JavaScript Date プロトタイプ属性
- 前のページ parse()
- 次のページ setDate()
- 上一階層に戻る JavaScript Dateリファレンスマニュアル
定義と使用法
prototype
コンストラクタ関数は、Date()オブジェクトに新しい属性とメソッドを追加することができます。
コンストラクタ属性では、すべてのデートオブジェクトに属性とその値がデフォルト値として割り当てられます。
コンストラクタメソッドでは、すべてのデートオブジェクトがこのメソッドを使用できます。
注記:Date.prototype は、単一の日付オブジェクトを参照するのではなく、Date() オブジェクト自体を参照しています。
注記:Prototype は、すべての JavaScript オブジェクトに適用されるグローバルオブジェクト構築子です。
インスタンス
新しい日付メソッドを作成し、日付オブジェクトに myProp という month-name 属性を提供します:
Date.prototype.myMet = function() { if (this.getMonth() == 0){this.myProp = "January"}; if (this.getMonth() == 1){this.myProp = "February"}; if (this.getMonth() == 2){this.myProp = "March"}; if (this.getMonth() == 3){this.myProp = "April"}; if (this.getMonth() == 4){this.myProp = "May"}; if (this.getMonth() == 5){this.myProp = "June"}; if (this.getMonth() == 6){this.myProp = "July"}; if (this.getMonth() == 7){this.myProp = "August"}; if (this.getMonth() == 8){this.myProp = "September"}; if (this.getMonth() == 9){this.myProp = "October"}; if (this.getMonth() == 10){this.myProp = "November"}; if (this.getMonth() == 11){this.myProp = "December"}; };
Date オブジェクトを作成し、myMet メソッドを呼び出します:
var d = new Date(); d.myMet(); var monthname = d.myProp;
文法
Date.prototype.名前 = 値
技術的詳細
JavaScript バージョン: | ECMAScript 1 |
---|
ブラウザのサポート
属性 | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
prototype | サポート | サポート | サポート | サポート | サポート |
- 前のページ parse()
- 次のページ setDate()
- 上一階層に戻る JavaScript Dateリファレンスマニュアル