Elaborate on javascript Functions

Source: Internet
Author: User

Javascript Functions are a strange thing. After some time, you will be confused and cannot understand what it is. Are you confused because some javascript Functions have no names? Are you crazy because javascript function parameters have no types? Is it because javascript Functions exist as expressions. It is precisely because of these troubles that javascript Functions are worth looking for. I want to elaborate on functions from the composition of functions. This sounds like a nonsense. I want to talk about everything from composition, however, you cannot figure out the form of javascript Functions, so here I will elaborate on the composition of a standard function. 1. function names are generally named in object-oriented languages, but javascript Functions are not necessarily called anonymous functions or functions. It is like an expression, and the left value of this expression can be used to call a function, or stored in a variable and passed to other functions. It does not need to create a new object every time it is called. [Javascript] var f = function (x) {return x * x;}; alert (f (6); The above example is displayed in the dialog box 36 2. parameters of the javascript function are also amazing. If the parameters entered in the javascript function are inconsistent with those defined in the function (for example, the number of parameters is inconsistent), the program will not make errors, sometimes it can even be called successfully, which is unimaginable for the java language, but javascript Functions can. For example, in the above example, we call [javascript] alert (f (6, 7) and the result is still 36. Although this will not cause errors, we still want the function to be called correctly to ensure that the call is consistent with the number of parameters defined by the function. The Arguments object can be used to obtain the number of parameters of the function caller. Modify the preceding code. [Javascript] var f = function (x) {if (arguments. length> 1) {return "out of range";} else {return x * x ;}}; alert (f (6, 7); process and obtain the number of parameters of function callers, you can also obtain the values of these parameters and modify the above Code: [javascript] var f = function (x) {if (arguments. length> 1) {return arguments [0] * arguments [1]; [javascript] view plaincopy} else {return x * x ;}}; alert (f (6, 7 )); can you guess the result? Yes, it is 42.

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.