Two defining methods of functions in JavaScript and function variable assignment

Source: Internet
Author: User

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>

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.