您好,欢迎来到刀刀网。
搜索
您的当前位置:首页while循环遍历数组的方法

while循环遍历数组的方法

来源:刀刀网


在看jQuery源码时发现了这段代码, 自己试了一下, 简单的记录下来.

var arr = [ 'a', 'b', 'c', 'd' ],
 i = 0;while( arr[i++] ){ 
 console.log( arr[i] );
 /*
 
输出 b c d undefined */ }

代码执行顺序为:
(1)判断while(arr[i])是否存在,若存在,执行 (2) (3)
(2) i++
(3) console.log( arr[i] )

所以, 可以写成 console.log( arr[i-1] )

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务