any

any([iterator = Prototype.K[, context]]) -> Boolean

Enumerable 中的元素有一个或多个等价于 true,则返回 true,否则返回 false。参数 iterator 是一个函数对象,该参数可选,若指定该参数, 则根据参数所定义的规则判断元素等价的 bool 值。

译注:Prototype.K 是 Prototype 预设的一个函数对象,该函数接受一个参数,并直接返回该参数值。

显然,在发现某一元素等价的 bool 值为 true 后,程序会自动终止循环。如果未指定 iterator 参数,则直接判定元素等价的 bool 值,否则,将元素传递给 iterator 指定的函数,并根据返回的值判断等价的 bool 值。

可选的 context 参数是 iterator 要绑定的对象,若设定该参数,iterator 中的 this 关键字将指向 context 对象。

样例

[].any() 
// -> false (空数组,没有任何一个元素等价于 true) 
$R(0, 2).any() 
// -> true (对于第二次循环,2 等价于 true) 
[2, 4, 6, 8, 10].any(function(n) { return 0 == n % 3; }) 
// -> true (对于元素 6,iterator 返回 true:数组有一个或多个元素的值为 3 的倍数) 
$H({ opt1: null, opt2: false, opt3: '', opt4: 'pfew!' }).any(function(pair) {
	return pair.value; 
}) 
// -> true (幸好有 opt4/'pfew!' 名值对,它的值等价于 true) 

参见

如果想要判断 Enumerable 中的所有元素是否均符合指定的条件,请使用 all