Javascript 中的返回函数及其工作原理? [英] Return function in Javascript and how it works?
问题描述
我正在尝试优化和现有的一段 WebGl JavaScript 代码,代码的瓶颈是在带有 return
的 if 语句的末尾但是,return 语句似乎没有返回任何东西.
I'm trying to optimize and existing piece of WebGl JavaScript code, and the bottleneck of the code is at an if statement with return
at the end however, the return statement doesn't seem to return anything.
function render() {
if (object.loading) return; // there is no value after return
// Loading is a boolean indicating if information is still being parsed from a file or not
}
这是我第一次看到具有 return 语句的代码,但没有指定在其后返回的后续变量.
This is the first time I'm seeing code that has a return statement without a succeeding variable specified to be returned after it.
如果没有 return
,程序就会失败,所以它在那里绝对重要.
Without the return
the program fails so its definitely important it is there.
JavaScript 是否有特殊情况,可以使用 return
而不指定其后返回的内容?
Does JavaScript have a special situation in which return
can be used without specifying what is being returned after it?
推荐答案
如果使用 return
后没有值,则返回值 undefined
.这用于停止函数而不返回值.如果函数结束时没有 return 语句,它也会返回 undefined
.
If you use return
with no value after it, it returns the value undefined
. This is used to stop a function without returning a value. A function will also return undefined
if it ends without a return statement.
这篇关于Javascript 中的返回函数及其工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!