Examples of callback functions and anonymous functions in Javascript

  • 2020-03-30 02:54:41
  • OfStack

 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
<script type="text/javascript"> 
 
//= = = = = = = = = = = = = = = = = = ordinary function callback = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//Callback the action to be performed
function callback(){ 
alert(" Please help me go to the express "); 
} 
//I was about to do
function goShopping(a,fun){ 
alert(" I went to shopping the "); 
//Something to do before 10 o 'clock
if(a<10){ 
fun(); 
} 
} 
//The callback test
goShopping(9,callback); 
//This small example is a scenario in which a callback function is used: while some function is executing, without knowing what the future might hold,
//And at the same time, when certain conditions are met, you do something else, which is you destroy the function.
//= = = = = = = = = = = = = = = = = = ordinary function callback = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//= = = = = = = = = = = = = = = = = anonymous function callback = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//I was about to do
function goShopping(a,fun){ 
alert(" I went to shopping the "); 
//Something to do before 10 o 'clock
if(a<10){ 
fun(); 
} 
} 
goShopping(9,function(){ 
alert(" Help me to the express "); 
}); 
//= = = = = = = = = = = = = = = = = anonymous function callback = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
//= = = = = = = = = = = = = = = = = anonymous functions themselves call = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
function(){ 
alert(" I have no name. How does it work "); 
}(); 
//=============== =====
function(name){ 
alert(" I am a "+name); 
function test(){" I'm a secret agent! "}; 
//To call the internal test externally
window.test=test; 
}("javaScript"); 
//Calls the inner function of the self-tuning function
test(); 
//So jquery and other js frameworks are written in accordance with the above way oh.
//= = = = = = = = = = = = = = = = = anonymous functions themselves call = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
</script> 
</head> 
<body> 

</body> 
</html> 

Related articles: