我是PHP和JavaScript的初学者,并且对“没有价值的回报”有疑问。
#codeA 和 #codeB 的含义相同吗? 我想知道“函数内部没有值的返回”的作用。
#codeB 和 #codeC 之间是否有区别? 我想知道示例代码中的“返回” 和“退出” 之间是否存在差异。
如果 #codeB 和 #codeC 具有相同的功能,则首选哪种格式?
答案“ 1、2、3”在JavaScript中是否相同?
std::byte
function doFunction() {
if(!conditionA) {
//do something
}
}
function doFunction() {
if(conditionA) return;
//do something
}
答案 0 :(得分:3)
回答你
以下:
许多不同之处在于之后发生的事情,例如:
codeA
function doFunction() {
if(!conditionA) {
//do something -- only runs when conditionA is false
}else{
//do something else -- only runs when conditionA is true
}
//do something something else -- runs rather true or false
//this could be before
}
doFunction();
//do something something something else -- runs rather true or false
codeB
function doFunction() {
//do something something else -- runs rather true or false
if(conditionA) return;
//do something -- only runs when conditionA is false
//do something else -- only runs when conditionA is false
}
doFunction();
//do something something something else -- runs rather true or false
codeC
function doFunction() {
//do something something else -- runs rather true or false
if(conditionA) exit;
//do something -- only runs when conditionA is false
//do something else -- only runs when conditionA is false
}
doFunction();
//do something something something else -- only runs when conditionA is false
因此,从有限的角度来看,它们是相同的,但是B在conditionA is false
时不执行任何操作。对于C,如果确实是您的脚本,则结束,如果不结束,则在执行退出函数do something something something else
后继续执行。
根据情况使用哪种。