常见错误:
a = myfunction;
...而不是:
a = myfunction();
有什么办法可以避免这种情况吗?
如果我们在对象内有这个函数,我可以对对象说些什么以避免返回函数并需要执行吗?
最佳答案
您可以使用typeof
来检查它是否是一个函数:
a = myfunction;
var result;
if (typeof a == "function")
// a was set to myfunction, not myfunction()
result = a();
else
// a was presumably set to myfunction() and not myfunction
result = a;
这样,如果它确实是一个函数,您就可以调用该函数,否则您可以用它做其他事情,假设这就是您想要做的事情。
关于javascript - 获取函数而不是运行函数时引发错误的任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232941/