Javascript 通过内联函数调用将对象返回到ES6/Reactjs中的另一个对象时出错
在React应用程序中尝试返回包含函数/方法调用对象的对象时,我得到:Javascript 通过内联函数调用将对象返回到ES6/Reactjs中的另一个对象时出错,javascript,reactjs,ecmascript-6,es6-class,Javascript,Reactjs,Ecmascript 6,Es6 Class,在React应用程序中尝试返回包含函数/方法调用对象的对象时,我得到: Syntax error: this is a reserved word 导致此错误的代码是: const object = { this.methodOne(), this.methodTwo(), this.methodThree() }; 我想将对象返回到一个对象中,但语法错误似乎无法识
Syntax error: this is a reserved word
导致此错误的代码是:
const object = {
this.methodOne(),
this.methodTwo(),
this.methodThree()
};
我想将对象返回到一个对象中,但语法错误似乎无法识别我正在尝试调用这些函数来创建如下对象:
methodOne(){
return {foo: "bar"}
}
与我的类不同的函数也会出现同样的问题。答案非常简单。如果您不想用对象键声明它们,而是希望它们使用直接在对象中返回的键值对,则必须使用扩展运算符:
const object = {
...this.methodOne(),
...this.methodTwo(),
...this.methodThree()
};
这个ES6表示法告诉JavaScript使用对象中的键/值对返回的对象 答案很简单。如果您不想用对象键声明它们,而是希望它们使用直接在对象中返回的键值对,则必须使用扩展运算符:
const object = {
...this.methodOne(),
...this.methodTwo(),
...this.methodThree()
};
这个ES6表示法告诉JavaScript使用对象中的键/值对返回的对象