JavaScript Date prototype 属性
定义和用法
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.name = value
技术细节
| JavaScript 版本: | ECMAScript 1 |
|---|
浏览器支持
| 属性 | Chrome | IE | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| prototype | 支持 | 支持 | 支持 | 支持 | 支持 |