This article mainly introduces the use of return in JavaScript, including the definition and writing of return. If you need it, you can refer to this article to introduce the use of return in JavaScript, including the definition and writing of return. For more information, see
Recently, I learned from my friends before school that many people have Vague understandings of the return usage and meaning in functions. Here I will write an article to discuss the return usage with you.
1 Definition
Return: return. The official return statement terminates the current function and returns the value of the current function. See the following sample code:
Title 《script》 function func1(){ while (true){ return 1; } }; alert(func1()); 《script》
As you can see, I wrote an endless loop in the function and called it below. When the return statement is not written, the browser will always execute the statements in the loop, and it will be stuck;
After the return statement is written, the function is directly interrupted and a value 1 is returned to the function, which means that after the function is executed, the function body is assigned a value as the return value of the function, 1 will be returned here;
2 Writing Method
The official definition of return can be followed by a value, that is, it can be followed by any data type, number, String, object in javascript. Of course, another function is returned, for example:
TitleScript function func1 () {return function () {alert (1) ;}}; alert (func1 ());//! Func1 (); this annotation is returned by calling the UDF script 14.1516 1718
Example image:
Now that we can return a function, we will rewrite the following code into a callback function:
Original code:
TitleScript if (prompt ('input number 1') = 1 ){! Function () {alert ('lost right');} ()} else {! Function () {alert ('wrong set');} ()} script
After Rewriting:
TitleScript function func1 () {if (prompt ('input number 1') = 1) {return function () {alert (' ');}} else {return function () {alert ('incorrect ');}}}! Func1 (); script
The if statement is used to determine which function to execute before rewriting. After rewriting, the if statement is used to determine which function to return and then call it below. It doesn't make any sense, just to help us understand the return;
(2) Exercise 2
Use the return statement to implement a loop.
Idea: since a return statement can return a function, it can return itself and implement a loop function in subsequent calls;
TitleScript var I = 1; // define the cyclic Variable function func1 () {I ++; // change the cyclic variable if (I <5) {// parentheses are circular condition document. write (I +'
'); // Here is the loop body return func1 ();}}! Func1 (); // call the function script
The role of each part in the loop has been commented out in the code. You can test it yourself. The following code is executed:
We have done a very detailed research on the use of return. Next we will do some research on the callback function in the built-in javascript method. Here we take the sort () in the array as an example:
We all know that sort (); can write a callback function to specify the sorting rules for the array; sample code:
Title 《script》 var arr = [1,3,2,6,5]; arr.sort(function(a,b){ return a-b; }); console.log(arr); 《script》
Run:
We can see that when 1 is returned, the array order is reversed;
Then, we can draw the following conclusions:
When a-B <= 0, a is in front and B is in the back;
When a-B> 0 is, a is behind, B is before;
Here, you must have some questions about what a and B are. We can print the following code:
TitleScript var a = [1, 3, 2, 6, 5];. sort (function (a, B) {console. log ('A is: '+ a +' \ t B is: '+ B +'
'); Return a-B;}); console. log (a); script
Run:
Return a-B; we have analyzed the Ascending Order in detail, so return B-a; in descending order is simple. To put it bluntly, return-(a-B ); that is, on the basis of a-B, It is reversed to a descending order.
Here we can draw a general conclusion that the return value is a numerical value, sort (); The method sorts each part of the array based on the positive and negative values.