Home > Article > Web Front-end > What are the JavaScript pop-out methods?
JavaScript jump-out methods include: 1. Break statement, which can cause the running program to immediately exit the innermost loop or exit a switch statement; 2. Continue statement, which can terminate the current loop and enter the next time Loop; 3. Return statement, which can suspend the execution of the current function and return the function value.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
A great god in the front-end industry asked me to think about a question. He gave Big-man a piece of code, as follows:
function Seriously(options) { // if called without 'new', make a new object and return that if(window === this || !(this instanceof Seriously) || this.id !== undefined) { return new Seriously(options); } }
Will the return statement continue to be executed after it is executed? This is a problem that the master asked me to solve. Since return is mentioned, I will also solve the other two methods of ending the loop in JS: break and continue.
Break statement:
for(var i = 519; i < 550; i++) { if(i == 522) { break; } console.log(i); alert(i); document.write(i); }
Continue statement:
for(var i = 5; i >=0; i--) { if(i == 4 || i == 3 || i == 1) { continue; } console.log(i); alert(i); document.write(i); }
Return statement:
The return statement is used to specify the value returned by the function. The return statement can only appear in the function body, appearing anywhere else in the code will cause a syntax error!
for(var i = 1; i < 10; i++) { if(i == 8) { return; } console.log(i); alert(i); document.write(i); }
Execution resultUncaught SyntaxError: illegal return statement(...)
When the return statement is executed, even if there are other statements in the function subject, the function execution will stop!
<script type="text/javascript"> if(username == "") { alert("please input your username: "); return false; } else if (qq == "") { alert("please input your qq number: "); return false; } </script>
In the above example, when username is empty, it will not It will be executed downwards. In some form submissions, you can also prevent the default submission method by returning false and use the Ajax submission method instead, for example:
<form id="form" onSubmit="return false"> ... </form>
[Related recommendations: javascript learning tutorial 】
The above is the detailed content of What are the JavaScript pop-out methods?. For more information, please follow other related articles on the PHP Chinese website!