以下是我的代码,当我尝试控制台数组中定义的函数时,该代码抛出错误。让我知道我做错了什么。
var a = ['This is a string', {'name': 'Test User'}, 90, undefined, 'Another String', null, function(){return 'This is also valid'}];
for(var i=0; i<a.length; i++) {
if(typeof a[i] === 'function')
console.log(a[i]());
else
console.log(a[i]());
}
出现错误 -
TypeError: a[i] is not a function
最佳答案
从 else 中删除方法调用
for(var i=0; i<a.length; i++) {
if(typeof a[i] === 'function')
console.log(a[i]());
else
console.log(a[i]); //remove method call from here
}
关于javascript - 如何调用 JavaScript 数组中定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35053620/