我有一个简单的类,想在构造函数发起的一个方法中为只读属性赋值,但是它报错说:[ts] Cannot assign to 'readOnlyProperty' because it is a constant or a read-only property.
即使我从构造函数调用了process
方法,为什么我也不能给该属性赋值呢?
示例代码:
class C {
readonly readOnlyProperty: string;
constructor(raw: string) {
this.process(raw);
}
process(raw: string) {
this.readOnlyProperty = raw; // [ts] Cannot assign to 'readOnlyProperty' because it is a constant or a read-only property.
}
}
process()
只会从构造函数中调用? - Robby Cornelissen