JavaScript Date prototypeプロパティ

Date 对象参考手册JavaScript Date オブジェクト

インスタンス

Date オブジェクトの新しいメソッドを作成する:

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="Spetember"};
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;

monthname出力結果:

var d = new Date(); d.myMet(); document.write(d.myProp);

試してみる »

定義と使い方

prototype プロパティを使用すると、オブジェクトにプロパティとメソッドを追加できます。

プロトタイプを構築すると、すべての Date オブジェクトにデフォルトでプロパティとメソッドが追加されます。

注意:プロパティとメソッドはプロトタイプに追加できますが、オブジェクトに他のプロトタイプを割り当てることはできません。ただし、ユーザー定義オブジェクトには新しいプロトタイプを割り当てることができます。

注意:Prototype は、ほぼすべての JavaScript オブジェクトに適用されるグローバルプロパティです。


ブラウザサポート

Internet ExplorerFirefoxOperaGoogle ChromeSafari

すべての主要ブラウザが prototype プロパティをサポートしています。


構文

Date.prototype.name=value


Date 对象参考手册JavaScript Date オブジェクト

その他の拡張