【发布时间】:2020-02-12 09:33:01
【问题描述】:
JavaScript 新手。 我想检查 temp 是否是一个函数。另外我想知道为什么 typeof 在这种情况下不起作用:函数作为参数传递的情况。理解它是我的目的,所以请不要使用 jQuery。感谢所有的帮助。谢谢
function getParams(foo, bar) {
if (typeof bar === 'function') console.log("bar is a function");
console.log(typeof bar); // string: because i returned string. But why not a "function" ?
}
function temp(element) {
return element;
}
function runThis() {
getParams("hello", temp("world"));
}
runThis();
【问题讨论】:
-
你传递的不是函数,而是调用函数的结果。
-
@DaveNewton 是的,您正在传递一个返回值。这是一个字符串。
-
谢谢戴夫。有没有办法纯粹传递函数,而不是调用函数的结果?
-
getParams("hello", temp);
-
要传递带参数的函数,请传递一个匿名函数,该函数使用所需的参数调用您的函数:
getParams("hello", function() { temp("world"); });。
标签: javascript typeof