Home > Article > Web Front-end > JavaScript execution method for uncertain functions_javascript skills
This article analyzes the execution method of javascript for uncertain functions through examples. Share it with everyone for your reference, the details are as follows:
In JavaScript, sometimes we only know the name of a function, but we are not sure whether the function exists. How to determine whether the function exists and execute it. One method is to use eval() to perform concatenation of program strings, but this may cause performance problems. Another method is to use symbolic properties to access functions, because functions are properties of the window object.
Use window[function name] to represent the function object, and use window[function name]() to execute or call the function.
Example:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>新建网页 1</title> </head> <body> <script language="javascript"> function input1_onChange(){ alert('input1_onChage executed.'); } var objId = 'input1'; if(window[objId +'_onChange']){ alert('There is the funtion'); }else{ alert('There is not the funtion'); } if(window[objId+'_onChange'] && typeof(window[objId+'_onChange'])=='function'){ window[objId+'_onChange'](); } var fun = window[objId+'_onChange']; if(fun && typeof(fun)=='function'){ fun(); } </script> </body> </html>
I hope this article will be helpful to everyone in JavaScript programming.