JavaScript Date prototype 屬性

Date 對象參考手冊 JavaScript 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 屬性使您有能力向對象添加屬性和方法。

當構造一個原型,所有的日期對象都會默認添加屬性和方法。

注意: 可將屬性和方法添加到原型中,但不能為對象分配其他原型。 但是,可以向用戶定義的對象分配新的原型。

注意: Prototype是一個全局屬性,這對於幾乎全部的JavaScript對象。


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要流覽器都支持prototype 屬性。


語法

Date.prototype.name=value


Date 對象參考手冊 JavaScript Date 對象