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