JavaScript Date prototype egenskaber
- Forrige side parse()
- Næste side setDate()
- Gå tilbage til forrige niveau JavaScript Date Reference Håndbog
Definition og brug
prototype
Konstruktionsfunktionen giver dig mulighed for at tilføje nye egenskaber og metoder til Date()-objekter.
Ved konfiguration af egenskaber vil alle datoobjekter blive tildelt egenskaberne og deres værdier som standardværdier.
I konstruktionsmetoden kan alle datoobjekter bruge denne metode.
Note:Date.prototype does not refer to a single date object, but to the Date() object itself.
Note:Prototype is a global object constructor that applies to all JavaScript objects.
Example
Create a new date method that provides a month-name property named myProp for the date object:
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"}; ;
Create a Date object and then call the myMet method:
var d = new Date(); d.myMet(); var monthname = d.myProp;
Syntax
Date.prototype.name = value
Technical details
JavaScript version: | ECMAScript 1 |
---|
Browser support
Egenskaber | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
prototype | Support | Support | Support | Support | Support |
Relaterede sider
Tutorial:JavaScript dato
Tutorial:JavaScript datoformat
Tutorial:JavaScript objektbygger
- Forrige side parse()
- Næste side setDate()
- Gå tilbage til forrige niveau JavaScript Date Reference Håndbog