我有两个 "exact" 函数,一个不起作用
I have two "exact" functions and one doesn't work
据我所知,我有两个功能完全相同。唯一的区别是一个是简洁的主体,另一个是 ES6 之前的长版。简洁的版本有效,而另一个则无效。加长版有什么问题?
//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');
}
() => something()
语法在 something()
之前插入一个隐含的 return。要使这些函数等价,请在函数中添加 return
:
function() {
return getSleepHours('monday') +
getSleepHours('tuesday') +
getSleepHours('wednesday') +
getSleepHours('thursday') +
getSleepHours('friday') +
getSleepHours('saturday') +
getSleepHours('sunday');
}
据我所知,我有两个功能完全相同。唯一的区别是一个是简洁的主体,另一个是 ES6 之前的长版。简洁的版本有效,而另一个则无效。加长版有什么问题?
//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');
}
() => something()
语法在 something()
之前插入一个隐含的 return。要使这些函数等价,请在函数中添加 return
:
function() {
return getSleepHours('monday') +
getSleepHours('tuesday') +
getSleepHours('wednesday') +
getSleepHours('thursday') +
getSleepHours('friday') +
getSleepHours('saturday') +
getSleepHours('sunday');
}