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

WBOY
Release: 2016-05-16 17:00:10
Original
1285 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!