原型 - Enumerable all() 方法
此方法確定是否所有元素都是 boolean 等效值為 true,無論是直接進行還是透過提供的迭代器計算。
可選的 context 引數是迭代器函式將繫結到的內容。如果使用,則迭代器內的 this 關鍵字將指向引數給定的物件。
語法
Iterator.all([context]);
返回值
如果迭代器中的所有值均為 true,則返回布林 true 值。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
alert( "[].all() : " + [].all() );
// true (empty arrays have no elements )
alert("$R(1, 5).all() : " + $R(1, 5).all() );
// true (all values in [1..5] are true-equivalent)
alert("[0, 1, 2].all() : " + [0, 1, 2].all() );
// false (with only one loop cycle: 0 is false-equivalent)
alert([9, 10, 15].all(function(n) { return n >= 10; }) );
//false (the iterator will return false on 9)
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
輸出
prototype_enumerating.htm
廣告