我们首先先创建一个index.js的文件在文件中定义一个数组,就像这样
const user = [
{ name: "李四", js: 89 },
{ name: "马六", js: 65 },
{ name: "张三", js: 78 },
];
const resust = user.every((user) => user.js >= 60);
console.log(resust);
使用node index.js
运行这段代码,我们可以看到输出的结果是
true
现在让我们来实现自己的every方法吧
const user = [
{ name: "李四", js: 89 },
{ name: "马六", js: 65 },
{ name: "张三", js: 78 },
];
Array.prototype.myEvery = function (fn) {
for (let i = 0; i < this.length; i++) {
if (!fn(this[i], i, this)) {
return false;
}
}
return true;
};
const resust = user.myEvery((user) => user.js >= 60);
console.log(resust);
标签:const,name,js,every,user,resust,手写
From: https://www.cnblogs.com/Jannik/p/17801898.html