JavaScript中every()方法的用途是什么?
JavaScript数组的每个 方法都会测试数组中的所有元素是否通过提供的函数实现的测试。
以下是参数-
callback-测试每个元素的函数。
thisObject- 执行回调时用作此对象的对象。
示例
您可以尝试运行以下代码来学习如何使用JavaScript中的every()方法-
<html>
<head>
<title>JavaScript Array every Method</title>
</head>
<body>
<script>
if (!Array.prototype.every) {
Array.prototype.every = function(fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this && !fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
}
function isBigEnough(element, index, array) {
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
document.write("First Test Value : " + passed );
passed = [12, 54, 18, 130, 44].every(isBigEnough);
document.write("<br>Second Test Value : " + passed );
</script>
</body>
</html>热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短