即使原型(prototype)本身实际上没有返回任何内容,只是操作 DOM,在原型(prototype)末尾添加返回是否会产生影响?
MyObj.prototype.DoStuff = function () {
// do stuff here... may or may not return a value
return; <-- code in question
}
谢谢!
最佳答案
如果您不包含 return
语句,JavaScript 中的所有函数都会隐式返回 undefined
。由于您包含了一个没有值的 return
语句,该函数仍将返回 undefined
,就像您未使用 return
语句一样。
详细信息请参见 in the spec :
Syntax
ReturnStatement :
return ;
return
[no LineTerminator here] Expression;
Semantics
...
1. If the Expression is not present, return (return, undefined, empty).
因此,您通常会在您的案例中省略 return
语句。仅当函数实际返回值时才有必要。
关于javascript - 在原型(prototype)末尾添加 return,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071084/