Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> Insert title here </title>
<Script type = "text/javascript">
/* I Summary:
1. function names can be used as variables, values can be assigned, and values can be passed.
2. The function name is passed to another function as a parameter.
*/
// ================================ Two defining methods of functions in js and function variable assignment ============ ==========
// Define a function in javascript to define a variable.
// Weak type of variables in js.
// ---------- 1
// Function add1 (){
// Alert ("add1 ");
//}
// Then the function can be used as a variable as a parameter, which is the first address that add1 points to the code block stored in the memory.
Var add1 = new Function ("alert ('add1');"); // -------- 2
// The above 1 and 2 statements are completely equivalent. They are two methods for declaring functions in js.
// In fact, add1 points to the first address stored in the memory of this function code block.
// As for how to store, stack, or stack, we will not study it here.
Var addtt = add1; // The function name can be assigned a value or a value when the variable is used.
// Addtt points to the function body
Addtt ();
// ================================ Two defining methods of functions in js and function variable assignment ============ ==========
// ============================ Pass the function variable as a parameter ===================== ==================
// Basic format:
Function add2 (fun ){
// Pass the function name as a parameter
Fun ();
}
Add2 (add1 );
//--------------------------------------
// The function name is used as a parameter, and the parameter is also accepted for demonstration.
Function add (){
Return n + 10;
}
// A: Number, fun: Function
Function addTest (a, fun ){
Var t = fun ();
Return t;
}
Var tt = addTest (22, add); // This is also OK.
Alert (tt );
// ============================ Pass the function variable as a parameter ===================== ==================
</Script>
</Head>
<Body>
</Body>
</Html>