import Foo from '(module foo)';
class Bar extends Foo {
test2() {
console.log(this[Bar.PROPERTY]);
}
}
export default Bar;
module main:
import Bar from '(module bar)';
new Bar.test2();
奇技淫巧版:
class Protectable {
static fetchSymbols(Klass, protectedSymbols) {
if (!(Klass instanceof this))
throw new TypeError('PROTECTED_SYMBOLS are only accessible to sub classes');
if (!Object.prototype.hasOwnProperty.call(Klass, 'receiver'))
throw new TypeError('receiver is not defined on Klass');
Object.keys(protectedSymbols).forEach(name => {
Klass.receiver(name, protectedSymbols[name]);
});
}
}
Object.freeze(Protectable);
module.exports = Protectable;
module foo:
module bar:
module main:
奇技淫巧版:
这个方案可以实现,
传统方式实现就很简单了
z就是类中的私有变量
应该没有吧
要么公有要么用奇技淫巧实现私有