Two ways of defining functions in JavaScript and function variable Assignment _ basics

Source: Internet
Author: User
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>insert title here</title>
<script type= "Text/javascript" >
/*i Summary:
1. Function name can be used as a variable, you can assign values, you can transfer values
2. Function name as parameter, passed to another function
*/
Two definitions of functions in ==================JS and function variable assignment ================
Defining a function in JavaScript can be understood as defining a variable
JS variables in the weak type of OH.
----------1
function Add1 () {
Alert ("Add1");
//}
So the function, as a variable, can be used as a parameter, and ADD1 points to the first address that this block of code stores in memory.
var add1=new Function ("alert (' Add1 ');"); /--------2
The above 1, 2 of the writing is completely equivalent to the declaration of a function in JS two ways
In fact, it is add1 that points to the function code block, the first address stored in memory.
As for specific how to store, heap or stack, here do not do more research.

var addtt=add1;//function name when variable is used, you can assign a value, or you can pass a value
ADDTT points to the function body
ADDTT ();
Two definitions of functions in ==================JS and function variable assignment ================
================== pass function variables as arguments ===========================
Basic format:
function Add2 (fun) {
Passing the function name as a parameter
Fun ();
}
ADD2 (ADD1);

//--------------------------------------
Function masterpiece parameters are used, while accepting parameters of the case demo
function Add (a) {
return n+10;
}
A: Numbers, fun: functions
function Addtest (a,fun) {
var t=fun (a);
return t;
}
var tt=addtest (22,add); this is OK.
Alert (TT);
================== pass function variables as arguments ===========================
</script>
<body>

</body>
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.