【发布时间】:2017-10-13 01:07:05
【问题描述】:
我想像在c中一样使用javascript构建一个类,主要问题是private属性。
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
所以我想检测来自tree.private_var 的访问并返回unaccessible,然后this.private_var 返回5。
我的问题是:有没有办法在 javascript 中设置私有属性?
编辑:我是这样看的
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 无法访问?
但是
【问题讨论】:
-
但是什么?您似乎没有完成问题。
标签: javascript private