However, the disadvantage is that prototype functions cannot access Tree private variables. Is there any way to do this?
, , . , . .
, smallBranches private - , :
var Tree = function() {
var smallBranches = 10;
...
this.getSmallBranches = function() { return smallBranches; };
};
Tree.prototype.countBranches = function() {
console.log(this.getSmallBranches());
};
, this.smallBranches, getter:
var Tree = function() {
var smallBranches = 10;
Object.defineProperty(this, 'smallBranches', {
get() { return smallBranches; }
});
}
Tree.prototype.countBranches = function() {
console.log(this.smallBranches);
this.smallBranches = 42;
};
user663031