Was wondering if it's possible to make the argument passed to the constructor below private? I tried wrapping in a function, but no success.. But basically, I shouldn't be able to change the value of test.one
after a value is already set.
class Test {
constructor(one){
this.one = one;
}
log(){
console.log(this.one);
}
}
const test = new Test(1);
test.log();
您可以使用#个名称(哈希名称)来使用私有实例字段。您的情况如下:
Good reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_fields#Private_instance_fields