Ecmascript 6 试图分配给只读属性ECMAScript React Native

Ecmascript 6 试图分配给只读属性ECMAScript React Native,ecmascript-6,react-native,Ecmascript 6,React Native,我正在尝试为我的组件中声明的数组赋值。不幸的是,抛出了异常 TypeError: Attempted to assign to readonly property 即使我删除了strict模式,仍然会引发异常。请有人指导我如何使变量既可读又可写?谢谢 代码: class RootView extends Component { cachedData : []; //declared array here //trying to assign dictionary in some fu

我正在尝试为我的
组件中声明的数组赋值。不幸的是,抛出了异常

TypeError: Attempted to assign to readonly property
即使我删除了
strict
模式,仍然会引发异常。请有人指导我如何使变量既可读又可写?谢谢

代码:

class RootView extends Component {

  cachedData : []; //declared array here


//trying to assign dictionary in some function

someFunction(results) {

    this.cachedData[this.state.searchString.length - 1] = results;
    //exception raised here
}

}


你的语法不正确。将其添加到构造函数中

class RootView extends Component {

  constructor() {
    super();
    this.cachedData = [];
  }
  someFunction(results) {
    this.cachedData[this.state.searchString.length - 1] = results;
  }
}
如果您的transpiler支持(阶段0),则可以使用以下功能:

class RootView extends Component {
  cachedData = [];
  someFunction(results) {
    this.cachedData[this.state.searchString.length - 1] = results;
  }
}

this.cachedData=[]工作!!但是
cachedData=[]不工作。非常感谢你。您能告诉我语法是怎么错的吗?您有
cachedData:[]
,它用于为对象分配属性。