Chr*_*lli 2 javascript arrays multidimensional-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
];
Run Code Online (Sandbox Code Playgroud)
这不起作用:
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;
}
];
})(),
// ...
];
Run Code Online (Sandbox Code Playgroud)
我希望stepFunc是一个函数数组的数组.在第一级,我想创建一个具有自己的数据的闭包.为什么stepFunc [1]"未定义"?
您可能在隐式语句终止(即隐式分号插入)上遇到了绊脚石.后行return完全忽略并且不返回任何内容.这有效:
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;
}
];
})()
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
930 次 |
| 最近记录: |