/*
* @Author: FengKongLi
* @Date: 2022-01-07 11:23:07
* @Last Modified by: FengKongLi
* @Last Modified time: 2022-01-07 13:22:38
*/
var users = [{ id: 1, name: "a" },
{ id: 2, name: "b" },
{ id: 3, name: "c" }]
Array.prototype.myFind = function (comditionFunc) {
// 相当于以下
/* var comditionFunc =function(item,index){
return item.id===4
} */
for (var i = 0; i < this.length; i++) {
if (comditionFunc(this[i], i)) {
return this[i]
}
}
}
var result = users.myFind(function (item) {
return item.id === 1;
})
console.log(result);
重构ES6中的find()
最新推荐文章于 2024-11-02 19:19:35 发布