码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 终止函数执行操作

时间:2015-04-10 01:13:14      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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‘便会终止。

javascript 终止函数执行操作

标签:

原文地址:http://www.cnblogs.com/caroline4lc/p/4412924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!