JavaScript:多线箭头函数表示returns一个对象
JavaScript: Multi line arrow function that returns an object
有人提示我跟进,
说明
编写一个名为 gemInfo 的多行箭头函数,它接受三个参数,gem 类型、gem 大小和 gem 颜色。将 gemInfo 函数 return 设置为这三个键的参数值的对象,gemType,gemSize,gemWeight。
- 应该使用箭头函数
- 应该是多行函数
function gemInfo(type, size, color){
var obj = {
type: gemType,
size: gemSize,
color: gemColor
};
return () => obj;
}
这就是我目前所知道的,我对自己的错误感到茫然,有人可以给我任何指导吗?
多行箭头函数如下所示
const gemInfo = (gemType, gemSize, gemWeight) => {
return {
gemType,
gemSize,
gemWeight
};
}
的官方文档
在您的代码中,function gemInfo(...) { ... }
不是 arrow function, it's a function declaration。此外,您的 return 值是 function
, 而不是 object
。
要 return 使用箭头函数的对象,请将 return 值括在括号中。
const gemInfo = (gemType, gemSize, gemColor) => ({
gemType,
gemSize,
gemColor,
});
const myGem = gemInfo('diamond', 'big', 'black');
console.log(myGem);
有人提示我跟进, 说明
编写一个名为 gemInfo 的多行箭头函数,它接受三个参数,gem 类型、gem 大小和 gem 颜色。将 gemInfo 函数 return 设置为这三个键的参数值的对象,gemType,gemSize,gemWeight。
- 应该使用箭头函数
- 应该是多行函数
function gemInfo(type, size, color){
var obj = {
type: gemType,
size: gemSize,
color: gemColor
};
return () => obj;
}
这就是我目前所知道的,我对自己的错误感到茫然,有人可以给我任何指导吗?
多行箭头函数如下所示
const gemInfo = (gemType, gemSize, gemWeight) => {
return {
gemType,
gemSize,
gemWeight
};
}
的官方文档
在您的代码中,function gemInfo(...) { ... }
不是 arrow function, it's a function declaration。此外,您的 return 值是 function
, 而不是 object
。
要 return 使用箭头函数的对象,请将 return 值括在括号中。
const gemInfo = (gemType, gemSize, gemColor) => ({
gemType,
gemSize,
gemColor,
});
const myGem = gemInfo('diamond', 'big', 'black');
console.log(myGem);