Methods to implement arrays of functions in javascript

  • 2020-03-30 01:04:13
  • OfStack

One of the biggest differences between js and languages like Java is that functions are also treated as data and can operate like an object in Java. And since js doesn't do type checking, arrays can hold anything. So I was wondering if I could store functions in an array.

Achieve the following functions:

The function stores itself in an array after it is called (it may not be stored as an argument)

Then you can use a large function to manipulate the array and call the function inside.

I was going to use this method to do an Undo function once (where each function stores itself in, and if an Undo removes the last function and executes the entire queue), but found it too cumbersome. But an array of functions does the trick: paste a JSP
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>MothedList</title> 
</head> 
<body> 
<input type="button" onclick="ff()" /> 
<script type="text/javascript"> 
list = new Array; 
f1 = function f1(flage) { 
alert("f1"); 
if (flage) 
list.push(f1); 
} 
f2 = function f1(flage) { 
alert("f2"); 
if (flage) 
list.push(f2); 
} 
function ff() { 
f1(true); 
f2(true); 
f = list.pop(); 
f(false); 
f = list.pop(); 
f(); 
} 
</script> 
</body> 
</html> 

Related articles: