简体   繁体   中英

JavaScript: Multi line arrow function that returns an object

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?

A multiline arrow function would look like this

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

See the official documentation of Arrow functions

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM