1、如果終止一個(gè)函數(shù)的用return即可,實(shí)例如下:
function testA(){
alert('a');
alert('b');
alert('c');
}
testA(); 程序執(zhí)行會依次彈出'a','b','c'。
function testA(){
alert('a');
return;
alert('b');
alert('c');
}
testA(); 程序執(zhí)行彈出'a'便會終止。
2、在函數(shù)中調(diào)用別的函數(shù),在被調(diào)用函數(shù)終止的同時(shí)也希望調(diào)用的函數(shù)終止,實(shí)例如下:
function testC(){
alert('c');
return;
alert('cc');
}
function testD(){
testC();
alert('d');
}
testD(); 我們看到在testD中調(diào)用了testC,在testC中想通過return把testD也終止了,事與愿違return只終止了testC,程序執(zhí)行會依次彈出'c','d'。
function testC(){
alert('c');
return false;
alert('cc');
}
function testD(){
if(!testC()) return;
alert('d');
}
testD(); 兩個(gè)函數(shù)做了修改,testC中返回false,testD中對testC的返回值做了判斷,這樣終止testC的同時(shí)也能將testD終止,程序執(zhí)行彈出'c'便會終止。
新聞熱點(diǎn)
疑難解答
圖片精選