在一个在线课程中,Kyle Simpson 说下面的代码演示了在 javascript 中提升的必要性,因为如果没有提升,“其中一个函数总是会被声明为时太晚”。
a(1) // 39
function a(foo){
if (foo > 20) return foo
return b(foo+2)
}
function b(foo){
return c(foo) + 1
}
function c(foo){
return a(foo*2)
}
Run Code Online (Sandbox Code Playgroud)
但这工作得很好。
var a = function(foo){
if (foo > 20) return foo
return b(foo+2)
}
var b = function(foo){
return c(foo) + 1
}
var c = function(foo){
return a(foo*2)
}
a(1) // 39
Run Code Online (Sandbox Code Playgroud)
那么故事是怎样的呢?抛开调用的方便和放置,还有没有需要吊装的情况?
我一定是疯了。假设我有一个数组数组。我想过滤子数组并最终得到一个过滤子数组的数组。说我的过滤器是“大于 3”。所以
let nested = [[1,2],[3,4],[5,6]]
// [[],[4][5,6]]
Run Code Online (Sandbox Code Playgroud)
在一些下划线 jiggery-pokery 失败后,我尝试了常规 for 循环。
for (var i = 0; i < nested.length; i++){
for (var j = 0; j < nested[i].length; j++){
if (nested[i][j] <= 3){
(nested[i]).splice(j, 1)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这只会从第一个子数组中删除 1。我原以为 splice 会改变底层数组,并且会更新长度以解决这个问题,但也许不会?或者也许其他的东西完全出错了。可能很明显;没有看到它。任何花哨或简单的帮助都感激地接受。