大家好,我现在是这样的:
var Human=function(){
this._a=Math.random();
};
(function() {
var before_get = function(human) {
};
var before_set = function(human, v) {
};
Human.prototype={
get A(){
before_get(this);
return this._a;
},
set A(v){
before_set(this, v);
this._a=v;
}
};
})();
alert(new Human().A); // test
alert(new Human().A); // test
一切都很好,除了我不希望将变量_a暴露给原型以外的任何其他地方。好的,我进行了一些搜索并意识到这是不可能的,所以我想知道我们是否通常将其保留为这样(我的意思是我们只是让那些 _a 变量到处乱飞还是有更好的解决方案)?