Functions nested in JavaScript using the

Source: Internet
Author: User
Tags function definition

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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.