静态方法
class Foo { static classMethod() { return 'hello';
}
}静态常量
class Foo { constructor() { super();
} static get StaticData(){ return "default"
}
}只定义get方法不定义 set,有static set 则可修改了
私有成员
let _counter = new WeakMap();let _action = new WeakMap();class Countdown { constructor(counter, action) {
_counter.set(this, counter);
_action.set(this, action);
}
dec() { let counter = _counter.get(this); if (counter < 1) return;
counter--;
_counter.set(this, counter); if (counter === 0) {
_action.get(this)();
}
}
}let c = new Countdown(2, () => console.log('DONE'));
c.dec()
c.dec()// DONE使用WeakMap
const _counter = Symbol('counter'); const _action = Symbol('action');
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]();
}
}
}// properties whose keys are symbols:http://www.2ality.com/2016/01/private-data-classes.html
作者:proud2008
链接:https://www.jianshu.com/p/469bdd51d99a