简体   繁体   English

方法的回调函数内部和外部返回值的差异

[英]Difference in returning values outside and inside a callback function for a method

I have a method that executes the following function, yet I noticed I can use return in both ways. 我有一个执行以下功能的方法,但我注意到我可以同时使用return

Once inside the function: 进入函数后:

this.tasks.forEach(function(task) {
    return task.completed = true;
});

Once outside the function: 一旦超出功能范围:

return this.tasks.forEach(function(task) {
    task.completed = true;
});

Both times I get the correct result, yet I wonder if there is any difference. 两次我都得到正确的结果,但是我想知道是否有任何区别。

As far as the return statement inside the callback function goes, there is no difference, except for the fact that there is an unnecessary return statement in the first example. 就回调函数内部的return语句而言,没有什么区别,除了第一个示例中存在不必要的return语句。 The return value of the forEach callback function is unused. forEach回调函数的返回值未使用。

However, the return statement outside the callback function could make a functional difference, as it will return undefined in the function it is run in. undefined is the default return value, so you will only notice a difference if there is code after it that is now being skipped. 但是,回调函数外return语句可以使一个功能上的差异,因为它会返回undefined它在运行的功能。 undefined是默认的返回值,那么你只会注意到一个区别,如果有代码,后现在被跳过。 Of course, calling return inside the callback will not return the parent function, as your second example will. 当然,在第二个示例中,在回调内部调用return不会返回父函数。

Excerpt from the MDN page on forEach : 摘录自forEach的MDN页面

forEach() executes the callback function once for each array element; forEach()对每个数组元素执行一次callback函数; unlike every() and some() , it always returns the value undefined . every()some() ,它总是返回undefined值。

They're very different but not in a way that necessarily makes any difference in this case. 它们有很大的不同,但在这种情况下并不一定会有所不同。 Having return task.completed = true inside the function just has it return true every time, which is ignored by Array.prototype.forEach . 在函数内部具有return task.completed = true时,每次都会使它返回true,这将被Array.prototype.forEach忽略。 The other way, the result of forEach is returned, which is always undefined (it's a void function). 另一种方法是,返回forEach的结果,该结果始终是undefined (这是一个void函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在函数内部或外部为原型分配方法之间的区别 - difference between assigning method to prototype inside or outside the function 函数返回2个值-回调问题? - Function returning 2 values - callback issue? function 在 class 构造函数 function 中与方法在 class 内部但在构造函数 function 外部(在 js 中)有什么区别? - What is difference between function within class constructor function and the method Inside class but outside constructor function (in js)? 在回调内部将对象返回到调用者函数 - Returning object to caller function inside callback 在函数内部和外部定义变量的区别 - Difference of defining a variable inside and outside a function 反应钩子没有从库事件(FabricJS)的回调 function 中返回更新的 state 值 - React hooks not returning updated state values inside callback function from library events (FabricJS) 在JS函数中返回值 - Returning values inside a JS function 分配给函数内部和函数外部的属性有什么区别? - What is difference between properties assigned to function inside of function and outside the function? NodeJS:将回调 function 内部的值分配给它外部的变量 - NodeJS: Assign the value inside a callback function to a variable outside of it 我希望了解内部和外部回调函数的承诺行为 - I wish to understand the behavior of promise inside and outside callback function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM