Home >Web Front-end >JS Tutorial >javascript terminates function execution_javascript skills

javascript terminates function execution_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:00:101365browse

1. If you want to terminate a function, just use return. The example is as follows:
function testA(){
alert('a');
alert('b');
alert ('c');
}
testA(); When the program is executed, 'a', 'b', 'c' will pop up in sequence.

function testA(){
alert('a');
return;
alert('b');
alert( 'c');
}
testA(); Program execution will terminate when 'a' pops up.

2. Call other functions within a function. When the called function terminates, you also want the calling function to terminate. The example is as follows:
function testC(){
alert('c');
return;
alert('cc');
}

function testD(){
testC();
alert('d');
}
testD(); We see that testC is called in testD, and think in testC TestD is also terminated through return. Contrary to expectations, return only terminates testC. When the program is executed, 'c' and 'd' will pop up in sequence.

function testC(){
alert('c');
return false;
alert('cc');
}

function testD(){
if(!testC()) return;
alert('d');
}
testD (); The two functions have been modified. TestC returns false, and testD judges the return value of testC. In this way, when testC is terminated, testD can also be terminated, and the program execution will terminate when 'c' pops up.

Statement:
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