最简洁的答案是不。
在 真正的 答案是肯定的:JS引擎必须要通知一些功能已经完成了它的业务,这是由函数返回有所作为。这也是为什么说函数 “已返回” 而不是
“完成”的 原因。 缺少显式return语句的函数将返回,例如说没有返回值的C(++)函数(其签名反映了这一点)将返回: __
undefined``void
void noReturn()//return type void{ printf("%dn", 123); return;//return nothing, can be left out, too}//in JS:function noReturn(){ console.log('123');//or evil document.write return undefined;//<-- write it or not, the result is the same return;//<-- same as return undefined}同样,在JS中,就像大多数每种语言一样,您可以随意忽略函数的返回值,这是非常困难的:
(function(){ console.log('this function in an IIFE will return undefined, but we don't care');}());//this expression evaluates to:(undefined);//but we don't care在一些 非常 低的水平,在回报转换成某种跳跃。如果一个函数真的 什么也没有 返回,就无法知道什么以及何时调用下一个函数,或者调用事件处理程序等等。
回顾一下:不,一个JS函数不需要返回任何代码。但是就JS引擎而言:函数 总是
返回某些内容,无论是通过
return语句来显式还是隐式地。如果函数隐式返回,则其返回值将始终是未定义的。



