10 09/2014

javascript tutorial notes 2

最后更新: Wed Sep 10 2014 12:37:50 GMT+0800

Array every 方法

array.every(callback[, thisObject])

Tests whether all elements in the array pass the test implemented by the provided function.根据提供的函数测试 是否 所有数组元素都满足该条件

alert(a.every(function(el,i,arr){return el>0}));

找到 false 就立即(退出循环)返回 false,否则返回 true。下面例子返回 1-5

console.log(a.every(function(el,i,arr){console.log(el);return el<5})) <="" pre="">

回调的三个参数:element, index, array

every 不改变 array.

空数组 满足任意条件,甚至不可能的 >1 && <0

console.log([].every(function(el,i,arr){console.log(el);return el>1 && el<0})) <="" pre="">

The range of elements processed by every is set before the first invocation of callback. Elements which are appended to the array after the call to every begins will not be visited by callback. If existing elements of the array are changed, their value as passed to callback will be the value at the time every visits them; elements that are deleted are not visited.第一次调用前,数组元素就已经被处理。新添加或者修改数组元素,没用了。看下面例子

console.log(a.every(function(el,i,arr){a[0]=-1;console.log(el);return el>0}))
1... 7 true
[-1, 2, 3, 4, 5, 6, 7]
function isLetter(character) {
    var character=character.toLowerCase();
  return (character >= "a" && character <= "z");
var word = prompt('输入一个单词');