This article mainly introduces the function nesting use in JavaScript, is the basic knowledge in the JavaScript introduction study, the need friend may refer to the next
Before JavaScript1.2, the function definition is only allowed at the top level of global code, but 1.2 of JavaScript can be nested functions to define other functions as well.
A function definition that still exists can be circular or without restrictions within the condition. These restrictions apply only to function declarations and function statements in function definitions.
The function text (another feature introduced in JavaScript1.2) may appear in any JavaScript expression, which means that they can appear inside the IF Else statement.
Example:
Here is an example of our two nested functions. This may be a bit confusing, but it works perfectly well:
?
1 2 3 4 5 6 7 8 9 |
<script type= "Text/javascript" > <!--function hypotenuse (A, b) {function square (x) {return x*x;} Return math.sqrt (Square (a) + square (b)); }//--> </script> |
Note: Here, we are using sqrt from the mathematical class function.
Now, you can call the usual way to call this function as follows:
?
1 2 3 4 5 |
&L T;script type= "Text/javascript" > <!--hypotenuse (1, 2); This'll produce 2.2360//--> </script> |