Je veux construire une classe en utilisant javascript comme en c, le principal problème est l'attribut private
.Javascript define variable privée
var tree = {
private_var: 5,
getPrivate:function(){
return this.private_var;
}
};
console.log(tree.private_var);//5 this line want to return unaccessible
console.log(tree.getPrivate());//5
donc je veux détecter l'accès de tree.private_var
et retour unaccessible
et this.private_var
retour 5
.
Ma question est: Y at-il un moyen de définir l'attribut privé en javascript?
EDIT: J'ai vu cette façon
class Countdown {
constructor(counter, action) {
this._counter = counter;
this._action = action;
}
dec() {
if (this._counter < 1) return;
this._counter--;
if (this._counter === 0) {
this._action();
}
}
}
CountDown a;
a._counter
est pas accessible? mais
mais quoi? On dirait que vous n'avez pas fini la question. – Barmar