【发布时间】:2015-03-16 06:36:52
【问题描述】:
我有这个调用自己的自执行函数:
(function a(x){
if(x > 0){
x--;
console.log(x);
}
a(x);
})(5);
//outputs 4 3 2 1 0
这是正确的行为。但是如果我将这个函数传递给一个变量,我怎样才能实现相同的行为?
var a = (function (x){
if(x > 0){
x--;
console.log(x);
}
//a(x); outputs error
})(5);
【问题讨论】:
-
我很确定
a在这一点上是未定义的(顺便说一句) -
你的 IIFE 什么都不返回,所以
a是undefined -
这里的 a 不是函数,而是保存未定义函数的返回值
标签: javascript