我要检查,如果一个函数的定义(我不在乎怎么样,我的意思是它是可调用)
如何确定是否定义了JavaScript函数?
示例代码:
var functions = {
'alert':'alert',
'undefinedFunction':'undefinedFunction',
'ff.showAlert':'ff.showAlert'
};
var ff = {
showAlert: function() {
alert('the function works');
}
};
for (i in functions) {
console.log(i+' : '+typeof window[functions [i]]);
}
这将返回:
alert : function
undefinedFunction : undefined
ff.showAlert : undefined
console.log(typeof window.ff.showAlert); return function
Live demo
有没有办法以编程方式检查函数是否存在?
可能重复javascript函数存在(http://stackoverflow.com/questions/798340/testing-if-javascript-function-exists) –
不,没有,在他**知道的情况下**函数名,在我的情况我不要.. – skafandri