js的every和some的区别

代码:

[javascript] view plain copy
  1. <span style="font-family:Courier New;font-size:14px;"><script type="text/javascript">  
  2.   
  3.     var arr = [ 1, 2, 3, 4, 5, 6 ];  
  4.   
  5.     console.log( arr.some( function( item, index, array ){  
  6.         console.log( 'item=' + item + ',index='+index+',array='+array );  
  7.         return item > 3;  
  8.     }));  
  9.   
  10.     console.log( arr.every( function( item, index, array ){  
  11.         console.log( 'item=' + item + ',index='+index+',array='+array );  
  12.         return item > 3;  
  13.     }));  
  14.   
  15. </script></span>  

运行结果如下:



可以看到,some方法是碰到一个返回true的值时候就返回了,并没有继续往下运行,而every也一样,第一个值就是一个false,
所以后面也没有进行下去的必要了,就直接返回结果了。

原文地址:https://www.cnblogs.com/t0404/p/10290993.html