I have two functions that are, from what I can tell, exactly the same. The only difference is one is a concise body and the other is the pre-ES6 long version. The concise version works and the other doesn't. What's the problem with the longer version?
//this version of the getActualSleepHours function works
const getActualSleepHours = () =>
getSleepHours('monday')
getSleepHours('tuesday')
getSleepHours('wednesday')
getSleepHours('thursday')
getSleepHours('friday')
getSleepHours('saturday')
getSleepHours('sunday');
//this version of the getActualSleepHours function doesn't work
const getActualSleepHours = function() {
getSleepHours('monday')
getSleepHours('tuesday')
getSleepHours('wednesday')
getSleepHours('thursday')
getSleepHours('friday')
getSleepHours('saturday')
getSleepHours('sunday');
}
CodePudding user response:
The () => something()
syntax inserts an implicit return before something()
. To make these functions equivalent add a return
to the function:
function() {
return getSleepHours('monday')
getSleepHours('tuesday')
getSleepHours('wednesday')
getSleepHours('thursday')
getSleepHours('friday')
getSleepHours('saturday')
getSleepHours('sunday');
}