Straighten out JavaScript (17)-Functions

Source: Internet
Author: User
Function Name
 
Function fun () {alert (123);} fun (); // 123f = function () {alert (123);} f (); // 123msg = alert; MSG (123); // 123

  

Function return value

 
Function fun () {var num = 1; return num; // the function can have no return; if there is a laterCodeNum ++; return num;} var r = fun (); alert (r); // 1

  

Set parameters and actual parameters of the Function

 
/* Number of predefined parameters */function fun (A, B, C, D) {alert (fun. length);/* Number of predefined parameters */Alert (arguments. callee. length);/* can also be like this, arguments. callee identifies the current function */} fun (11, 22); // 4/4/* Number of actual parameters, traversal parameters */function fun (A, B, C, D) {alert (arguments. length); // the actual number of parameters for (VAR I = 0; I

This indicates the object that calls the function.

<Div name = 'mydiv 'onclick = "alert (this. Name);"> aaaaaa </div> // The mydiv

  

Caller indicates the function that calls this function.

 
Function fun1 () {return arguments. callee. caller. arguments [0]; // or write: fun1.caller. arguments [0];} function fun2 () {alert (fun1 ();} fun2 (11, 22); // 11

  

Call, apply

 
Function sum (a, B, c) {alert (A + B + C);} sum (1, 2, 3); // 6sum. call (null, 1, 2, 3); // 6sum. apply (null, [1, 2, 3]); // 6 // call and apply are both function call methods. Here, parameters other than null are function parameters; apply needs to write the parameter in the array; its purpose is to reset the first parameter. // The first parameter of call and apply indicates the object that calls the function, and null indicates that no call object exists. For example, function MSG (s) {alert (S + ''+ this. tostring ();} var STR = 'abc'; var num = 123; MSG. call (STR, 'Hi'); // Hi abcmsg. call (Num, 'Hi'); // Hi 123.

  

Create a function using the function class

VaR fun = new function ('alert (123) '); fun (); // 123var fun = new function ('a, B', 'Return A + B '); alert (fun (11, 22); // 33

  

A small example of closure

 
Function fun (x) {return function (y) {return X + Y;} var A, B, C; A = fun (1); B = fun (2 ); C = fun (3); alert (A (1); // 2 alert (B (2); // 4 alert (C (3); // 6

  

Eval: This is a global function that executes the JS Code in the string and returns the result.

 
VaR STR = '(1 + 2) * 3-1)/4'; alert (eval (STR); // 2

  

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.