我正在尝试了解范围如何在JS中运行。你能否告诉我,如果我理解这段代码是正确的:
for(var i = 0; i<5; i++){
setTimeout(function timeoutHandler() {
var i = i;
console.log(i); //undefined
})
}
console.log
打印undefined
5次。据我了解,当执行timeoutHandler
时,它有自己的本地范围和自己的i
变量。那么正确的是,在timeoutHandler
执行期间,解释器要求i
变量,发现它在本地范围内声明并使用此声明的变量进行赋值?这解释了为什么它是undefined
,但我不确定我是完全正确的。
感谢
UPD 我不需要这段代码才能正常工作,我只想了解为什么它在这种情况下的行为
答案 0 :(得分:0)
试图通过这个例子来理解它:
let
&#13;
使用ES6更容易:
使用var
代替let
。
由于setTimeout
在块中具有范围(循环的每次运行都是它自己的块),因此它不会在for(let i = 0; i<5; i++){
setTimeout(function() {
console.log(i); //will print the correct i
})
}
内获得另一个值。
> dput(av.rt)
structure(list(Reward = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("R", "U"), class = "factor"),
Congruency = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("con",
"inc"), class = "factor"), Subject = structure(c(1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L,
12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 18L,
18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 21L,
21L, 21L, 21L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 24L,
24L, 24L, 24L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 27L,
27L, 27L, 27L), .Label = c("9", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36"), class = "factor"), TTime.Shifted = c(565.618181818182,
605.669565217391, 554.683333333333, 655.486956521739, 577.247619047619,
631.409523809524, 591.419047619048, 757.270588235294, 520.191304347826,
607.617391304348, 495.290909090909, 648.842105263158, 500.9,
585.945454545455, 521.105882352941, 768.8, 553.009090909091,
622.078260869565, 564.636363636364, 696.094736842105, 484.6,
553.739130434783, 636.657142857143, 635.817391304348, 618.017391304348,
713.145454545455, 600.533333333333, 857.61, 437.582608695652,
526.572727272727, 445.390476190476, 531.345454545455, 565.104347826087,
613.746666666667, 609.714285714286, 614.87, 566.716666666667,
687.99, 588.694736842105, 655.555555555556, 462.53, 726.34,
611.8625, 803.2, 485.234782608696, 609.284210526316, 504.2,
647.46, 471.226086956522, 554.741666666667, 506.62, 631.009523809524,
617.991304347826, 623.252173913044, 655.895652173913, 659.85,
605.318181818182, 580.947826086957, 619.675, 657.857142857143,
523.22, 626.976470588235, 565.233333333333, 711.336842105263,
455.716666666667, 533.818181818182, 464.5, 663.505263157895,
549.172727272727, 608.85, 579.2, 602.952941176471, 646.383333333333,
626, 732.555555555556, 663.91, 469.48, 578.095238095238,
568.694736842105, 620.922222222222, 571.37, 603.386666666667,
650.109090909091, 632.233333333333, 584.363636363636, 643.811111111111,
594.961904761905, 723.82, 515.845454545455, 526.75652173913,
594.647619047619, 545.878260869565, 541.38, 550.15652173913,
538.778947368421, 618.175, 539.219047619048, 563.842105263158,
539.12380952381, 684.018181818182, 430.217391304348, 590.85,
439.547826086957, 605.634782608696, 591.933333333333, 673.742857142857,
633.441666666667, 762.022222222222)), .Names = c("Reward",
"Congruency", "Subject", "TTime.Shifted"), row.names = c(NA,
-108L), class = "data.frame")
&#13;