我知道map函数是如何工作的以及我应该如何使用map函数.但是在玩猴子代码时,我无法理解移位功能在这里是如何工作的?
var a = [0,1,2,3,4,5,6];
console.log(a);
a.map(function(){
a.shift();
});
console.log(a); // [4,5,6]Run Code Online (Sandbox Code Playgroud)
我现在完全糊涂了a[4,5,6]?
结果是[4,5,6],这只是因为在这种情况下减少了数组大小并且map函数没有运行7次.让我用ascii图说明:
map contains expected run times after shifted actual run times
--------------------------------------------------------------------------------------
[0,1,2,3,4,5,6] 7 [1,2,3,4,5,6] 1
[1,2,3,4,5,6] 6 [2,3,4,5,6] 2
[2,3,4,5,6] 5 [3,4,5,6] 3
[3,4,5,6] 4 [4,5,6] 4
Run Code Online (Sandbox Code Playgroud)
现在,map函数的预期运行时间是实际运行时间.因此,无需再次运行map函数.
希望,这清除了幕后的事情.
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |