为什么这个函数返回“未定义”?而不是阵列? [英] Why is this function returning "undefined" instead of the array?
本文介绍了为什么这个函数返回“未定义”?而不是阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这项工作:
var stepFunc =
[
//Step 0 (it doe not exist)
function(){console.log("Step 0");},
//Step 1
(function(){
var fFirstTime = true;
return function(){
console.log("Step 1");
if (fFirstTime){
//Do Something
}
}
})(),
// ... Other steps
];
这不起作用:
var stepFunc =
[ // Step 0
[
function(){console.log("Step 0");},
function(){console.log("Check Step 0");}
], //Step 1
(function(){
var fFirstTime = true;
return
[
function() { //Initialization function
console.log("Step 1");
if (fFirstTime){
//Do somrthing
}
}, function() {
return true;
}
];
})(),
// ...
];
我希望stepFunc是一个函数数组的数组。在第一级,我想创建一个具有自己的数据的闭包。为什么stepFunc [1]未定义?
I would like that stepFunc would be an array of arrays of functions. On the first level I want to create a closure that has its own data. Why is stepFunc[1] "undefined" ?
推荐答案
你可能在隐式语句终止,a.k.a。隐式分号插入方面遇到困难。 return
之后的行被完全忽略,并且不返回任何内容。这是有效的:
You're probably stumbling over implicit statement termination, a.k.a. implicit semicolon insertion. The line after return
is ignored completely and nothing is returned. This works:
var stepFunc = [
// Step 0
[
function(){console.log("Step 0");},
function(){console.log("Check Step 0");}
], //Step 1
(function(){
var fFirstTime = true;
return [ // <-- important to begin the return value here
function() {
console.log("Step 1");
if (fFirstTime){
//Do somrthing
}
}, function() {
return true;
}
];
})()
];
这篇关于为什么这个函数返回“未定义”?而不是阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文