| The code is as follows |
Copy Code |
| callback function 1 function Callback (A,B,C) { alert (A+B+C); } callback function 2 function Callback2 (a,b) { alert (A+B); } This method does some action, and then calls the callback function function DoCallback (Fn,args) { Fn.apply (this, args); }
function test () {
Dynamically calling methods, and passing parameters DoCallback (callback2,[' A ', ' B ']); DoCallback (callback,[' A ', ' B ', ' C ']); }
|
Note that the above Callback2 and the like can not be a string of OH.
What if you want to implement a dynamic call based on a string?
| The code is as follows |
Copy Code |
| Please look at the following code callback function 1 function Callback (A,B,C) { alert (A+B+C); } callback function 2 function Callback2 (a,b) { alert (A+B); } This method does some action, and then calls the callback function function DoCallback (Fn,args) { Fn.apply (this, args); }
function test () {
Dynamically calling methods, and passing parameters DoCallback (eval ("Callback2"), [' A ', ' B ']); DoCallback (callback,[' A ', ' B ', ' C ']); } |
Use eval ("Callback2") just fine Oh, kiss is not very simple ~