Objet javascript problème de getter, comment le rendre 'public'

randman -  
 randman -
Bonjour,
Voici mon soucis:

//---TEST
var Stats = function(hpMax,mpMax){
//this.hpMax=hpMax;// l' instance retourne la valeur
var hpMax=hpMax;//le getter renvoit 'undefined'
}
Stats.prototype.getHpMax=function(){
return(this.hpMax);
}
var mystats= new Stats(10,0);
//alert(mystats.hpMax);// si public marche
alert(mystats.getHpMax());// ?

Est que j'ai loupé quelque chose ? Comment rendre mon accesseur ..accessible?
Merci de vos réponses

1 réponse

  1. randman
     
    ça passe en mettant la fonction directement dans le constructeur, comme ceci:
    var Stats = function(hpMax,mpMax){
    //this.hpMax=hpMax;// l' instance retourne la valeur
    var hpMax=hpMax;//le getter renvoit 'undefined'
    this.getHpMax=function(){
    return(hpMax);
    }
    }

    var mystats= new Stats(10,0);

    alert(mystats.getHpMax());


    Si vous avez l'explication quand même je prends :)
    0