84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
网上看到一个javascript的算法。但不是懂运行原理,求指教。
var ints=[1,3,2,4,8,6,7,5]; for(i = 0;i < ints.length;i++) { setTimeout(function(num){ console.log(num); },ints[i],ints[i]); }
学习是最好的投资!
Understanding of a javascript algorithm - PHP Chinese website Q&A - Understanding of a javascript algorithm - PHP Chinese website Q&A
Take a look around and learn.
setTimeout有两种形式setTimeout(code,interval)setTimeout(func,interval,args) 所以第二个ints[i]就会作为参数传进去
setTimeout(code,interval)
setTimeout(func,interval,args)
ints[i]
javascript的执行上下文是采用运行期的,也就是说,如果不传ints[i]的话,会导致setTimeout里调用的number一直是数组的最后一项,也就是5。
遇到元素是 0 就死了. 0 只是推迟到最后.
Understanding of a javascript algorithm - PHP Chinese website Q&A - Understanding of a javascript algorithm - PHP Chinese website Q&A
Take a look around and learn.
setTimeout有两种形式
setTimeout(code,interval)
setTimeout(func,interval,args)
所以第二个
ints[i]
就会作为参数传进去javascript的执行上下文是采用运行期的,也就是说,如果不传ints[i]的话,会导致setTimeout里调用的number一直是数组的最后一项,也就是5。
遇到元素是 0 就死了.
0 只是推迟到最后.