Esempio
Fare un nuovo metodo di data che dà la data oggetto un immobile mese-nome chiamato myProp:
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"};
};
Fare un oggetto Date, quindi chiamare il metodo myMet:
var d = new Date();
d.myMet();
var monthname = d.myProp;
Il risultato di MonthName sarà:
Prova tu stesso " Definizione e l'utilizzo
Il costruttore prototipo consente di aggiungere nuove proprietà e metodi per la Date() dell'oggetto.
Quando si costruisce una proprietà, tutti gli oggetti di data sarà data la proprietà, e il suo valore, come di default.
Quando si costruisce un metodo, tutti gli oggetti di data avranno questo metodo disponibile.
Note: Date.prototype non si riferisce ad un singolo oggetto data, ma alla Date() dell'oggetto stesso.
Note: Prototype è un costruttore oggetto globale che è disponibile per tutti gli oggetti JavaScript.
Supporto browser
Proprietà | |||||
---|---|---|---|---|---|
prototype | sì | sì | sì | sì | sì |
Sintassi
Date.prototype. name = value
Dettagli tecnici
Versione JavaScript: | 1.1 |
---|
<JavaScript Data Object