JavaScript Date prototypePropriété
Exemple
Créer une nouvelle méthode d'objet date :
Date.prototype.myMet=function()
{
if (this.getMonth()==0){this.myProp="janvier"};
if (this.getMonth()==1){this.myProp="février"};
if (this.getMonth()==2){this.myProp="mars"};
if (this.getMonth()==3){this.myProp="avril"};
if (this.getMonth()==4){this.myProp="mai"};
if (this.getMonth()==5){this.myProp="juin"};
if (this.getMonth()==6){this.myProp="juillet"};
if (this.getMonth()==7){this.myProp="août"};
if (this.getMonth()==8){this.myProp="septembre"};
if (this.getMonth()==9){this.myProp="octobre"};
if (this.getMonth()==10){this.myProp="novembre"};
if (this.getMonth()==11){this.myProp="décembre"};
}
{
if (this.getMonth()==0){this.myProp="janvier"};
if (this.getMonth()==1){this.myProp="février"};
if (this.getMonth()==2){this.myProp="mars"};
if (this.getMonth()==3){this.myProp="avril"};
if (this.getMonth()==4){this.myProp="mai"};
if (this.getMonth()==5){this.myProp="juin"};
if (this.getMonth()==6){this.myProp="juillet"};
if (this.getMonth()==7){this.myProp="août"};
if (this.getMonth()==8){this.myProp="septembre"};
if (this.getMonth()==9){this.myProp="octobre"};
if (this.getMonth()==10){this.myProp="novembre"};
if (this.getMonth()==11){this.myProp="décembre"};
}
Créez un objet Date et appelez la méthode myMet de l'objet :
var d = new Date();
d.myMet();
var monthname = d.myProp;
d.myMet();
var monthname = d.myProp;
monthnameRésultat de sortie :
var d = new Date();
d.myMet();
document.write(d.myProp);
Essayez-le vous-même »
Définition et utilisation
La propriété prototype permet d'ajouter des propriétés et des méthodes aux objets.
Lors de la construction d'un prototype, tous les objets Date auront les propriétés et méthodes ajoutées par défaut.
Remarque :Des propriétés et méthodes peuvent être ajoutées au prototype, mais d'autres prototypes ne peuvent pas être assignés à des objets. Cependant, de nouveaux prototypes peuvent être assignés à des objets définis par l'utilisateur.
Remarque :Prototype est une propriété globale qui s'applique à presque tous les objets JavaScript.
Prise en charge des navigateurs
![]()
![]()
![]()
![]()
![]()
Tous les principaux navigateurs prennent en charge la propriété prototype.
Syntaxe
Date.prototype.name=value
Autres extensions
Objet Date JavaScript