【发布时间】:2015-07-18 20:46:08
【问题描述】:
我在尝试为我正在做的一些动画填充 JS 数组时遇到了这个问题....当我单击网页中的链接进行测试时,会调用以下 Javascript 函数:
function testing()
{
var funcArray = [];
var testFunc = function(){console.log("test function");}
funcArray.push(function(){console.log("hello there");});
funcArray.push(testFunc());
}
执行此操作时,我会在 JS 控制台中显示“测试功能”,但不会出现“你好”。为什么推送预定义的testFunc会导致输出,而不是第一次推送时的内联函数?
【问题讨论】:
-
去掉函数括号。只推名字,
funcArray.push(testFunc); -
因为您正在使用 () 执行函数
-
明白。非常感谢。
标签: javascript jquery arrays function