您好,欢迎访问一九零五行业门户网

javascript 终止函数执行操作_javascript技巧

1、如果终止一个函数的用return即可,实例如下:
function testa(){
    alert('a');
    alert('b');
    alert('c');
}
testa(); 程序执行会依次弹出'a','b','c'。
function testa(){
    alert('a');
    return;
    alert('b');
    alert('c');
}
testa(); 程序执行弹出'a'便会终止。
2、在函数中调用别的函数,在被调用函数终止的同时也希望调用的函数终止,实例如下:
function testc(){
    alert('c');
    return;
    alert('cc');
}
function testd(){
    testc();
    alert('d');
}
testd(); 我们看到在testd中调用了testc,在testc中想通过return把testd也终止了,事与愿违return只终止了testc,程序执行会依次弹出'c','d'。
function testc(){
    alert('c');
    return false;
    alert('cc');
}
function testd(){
    if(!testc()) return;
    alert('d');
}
testd(); 两个函数做了修改,testc中返回false,testd中对testc的返回值做了判断,这样终止testc的同时也能将testd终止,程序执行弹出'c'便会终止。
其它类似信息

推荐信息