[英]Why when apply Map.prototype.forEach will return undefined?
I wonder what does it mean by" Return undefined."
我想知道“返回未定义”是什么意思。 here?
这里?
It means the function forEach
will execute the provided function f
and nothing else, basically, the function forEach
doesn't return anything because its job is to loop the provided entries calling the callback function f
on every entry within the map. 这意味着
forEach
函数将执行提供的函数f
而不会执行其他任何操作,基本上,函数forEach
不返回任何内容,因为它的工作是在映射内的每个条目上循环调用提供的条目,并调用回调函数f
。
If the callback returns a value, this will be ignored. 如果回调返回一个值,则将忽略该值。
var undefinedValue = new Map([ ['foo', 3], ['bar', {}], ['baz', 2] ]).forEach(() => { console.log('Looping...'); return "HELLO WORLD!"; // This will be ignored! }); console.log("Value returned from forEach:", undefinedValue)
This just means that the effect of forEach
is entirely dependent on what you do inside the function f
, you don't get an overall returned value from the forEach
operation. 这仅意味着
forEach
的效果完全取决于您在函数f
所做的操作,您不会从forEach
操作中获得总体返回值。 If you did this: 如果您这样做:
let a = arr.forEach(myFunc);
...then a
would be undefined. ...那么
a
将是不确定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.