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

Fermé
randman - 4 août 2015 à 13:59
 randman - 4 août 2015 à 14:35
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
A voir également:

1 réponse

ç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