【发布时间】:2012-05-04 12:53:49
【问题描述】:
我想检查一个函数是否已定义(我不在乎如何,我的意思是它是可调用的)
示例代码:
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