最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: Multi line arrow function that returns an object - Stack Overflow

programmeradmin5浏览0评论

I was given a prompt to follow, Instructions

Write a multi-line arrow function called gemInfo that takes in three parameters, a gem type, gem size, and a gem color. Have the gemInfo function return an abject with the values of those parameters set to these three keys, gemType, gemSize, gemWeight.

  • Should use arrow function
  • Should be a multi-line function

function gemInfo(type, size, color){
  var obj = {
    type: gemType,
    size: gemSize,
    color: gemColor
  };
  return () => obj;
}

I was given a prompt to follow, Instructions

Write a multi-line arrow function called gemInfo that takes in three parameters, a gem type, gem size, and a gem color. Have the gemInfo function return an abject with the values of those parameters set to these three keys, gemType, gemSize, gemWeight.

  • Should use arrow function
  • Should be a multi-line function

function gemInfo(type, size, color){
  var obj = {
    type: gemType,
    size: gemSize,
    color: gemColor
  };
  return () => obj;
}

this is what i have so far and i am at a loss as to what i have wrong, can someone give me any guidance?

Share Improve this question asked Nov 21, 2017 at 6:30 Devin BowenDevin Bowen 892 silver badges9 bronze badges 2
  • What happened to gemWeight? – dork Commented Nov 21, 2017 at 6:34
  • Possible duplicate of ECMAScript6 arrow function that returns an object – IsmailS Commented Oct 4, 2018 at 12:13
Add a ment  | 

2 Answers 2

Reset to default 5

In your code, function gemInfo(...) { ... } isn't an arrow function, it's a function declaration. Also, your return value is a function, not an object.

To return an object using the arrow function, wrap the return value in parentheses.

const gemInfo = (gemType, gemSize, gemColor) => ({
  gemType,
  gemSize,
  gemColor,
});

const myGem = gemInfo('diamond', 'big', 'black');

console.log(myGem);

A multiline arrow function would look like this

const gemInfo = (gemType, gemSize, gemWeight) => {
  return {
    gemType,
    gemSize,
    gemWeight
  };
}

See the official documentation of Arrow functions

发布评论

评论列表(0)

  1. 暂无评论