The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*I Summary:
1. The function name can be used as a variable, can be assigned, and can be passed.
2. Function name is passed to another function as a parameter
*/
//========================= Two ways to define functions in js and function variable assignment========================================================================================
//Defining a function in javascript can be understood as defining a variable
//The variables in js are of weak type.
//--------------1
//function add1(){
//alert("add1");
//}
//The function can be used as a variable as a parameter, which is the first address stored in memory of this code block.
var add1=new Function("alert('add1');");//---------2
//The above 1 and 2 are completely equivalent, and are two ways to declare functions in js
//In fact, add1 points to the first address stored in memory in this function code block.
//As for how to store, heap or stack, I won’t do much research here.
var addtt=add1;// When a function name is used, you can assign a value or pass a value
//addtt points to the function body
addtt();
//========================= Two ways to define functions in js and function variable assignment========================================================================================
//=============================================================================================================================
//Basic format:
function add2(fun){
//Pass the function name as a parameter
fun();
}
add2(add1);
//-----------------------------------------------------------------------------------------------------------------------------
//The function name is used as parameters and the parameters are accepted at the same time.
function add(a){
return n+10;
}
//a: number, fun: function
function addTest(a,fun){
var t=fun(a);
return t;
}
var tt=addTest(22,add);//This way of writing is OK
alert(tt);
//=============================================================================================================================
</script>
</head>
<body>
</body>
</html>