将 'immediately invoking' 函数作为对象属性处理
Dealing with 'immediately invoking' functions as object properties
在我做的一个小型 React 项目中,我想通过调用一个函数来初始化 getInitialState
中的一个数组。参数引用 getInitialState
中的另外两个属性。我是这样做的:
getInitialState() {
return {
numRows: 55,
numCols: 47,
arrInit: function(){
this.grandArr = this.initBlank(this.numRows, this.numCols);
return this;
},
}.arrInit(); //Creates `grandArr` in the `state`
}
(this.initBlank
是组件中的一个方法,returns一个数组。)
链接 arrInit
的方式看起来很乱。有 better/cleaner 的方法吗?
您似乎希望 this
指代 state
对象和组件。我会进行设置,然后 return 您想要的对象:
getInitialState() {
const numRows = 55,
numCols = 47,
grandArr = this.initBlank(numRows, numCols);
return {numRows, numCols, grandArr};
}
在我做的一个小型 React 项目中,我想通过调用一个函数来初始化 getInitialState
中的一个数组。参数引用 getInitialState
中的另外两个属性。我是这样做的:
getInitialState() {
return {
numRows: 55,
numCols: 47,
arrInit: function(){
this.grandArr = this.initBlank(this.numRows, this.numCols);
return this;
},
}.arrInit(); //Creates `grandArr` in the `state`
}
(this.initBlank
是组件中的一个方法,returns一个数组。)
链接 arrInit
的方式看起来很乱。有 better/cleaner 的方法吗?
您似乎希望 this
指代 state
对象和组件。我会进行设置,然后 return 您想要的对象:
getInitialState() {
const numRows = 55,
numCols = 47,
grandArr = this.initBlank(numRows, numCols);
return {numRows, numCols, grandArr};
}