JavaScript Date prototype 属性
prototype函数允许您向Date()对象添加新属性和方法。构造属性时,将为所有日期对象提供属性及其值(默认值)。构造方法时,所有日期对象都将使用此方法。
注意: Date.prototype不引用单个日期对象,而是引用Date()对象本身。
注意: prototype是一个全局对象构造函数,可用于所有JavaScript对象。
实例:
创建一个新的日期方法,为date对象提供一个名为myMet的方法:
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;
尝试一下