forked from zzzzzhowie/practical-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsome.js
More file actions
22 lines (20 loc) · 668 Bytes
/
some.js
File metadata and controls
22 lines (20 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ES5循环实现 some 方法
const selfSome = function (fn, context) {
let arr = Array.prototype.slice.call(this)
// 空数组直接返回 false,数组的 every 方法则相反返回 true
if(!arr.length) return false
for (let i = 0; i < arr.length; i++) {
if(!arr.hasOwnProperty(i)) continue;
let res = fn.call(context,arr[i],i,this)
if(res)return true
}
return false
}
Array.prototype.selfSome ||(Object.defineProperty(Array.prototype, 'selfSome', {
value: selfSome,
enumerable: false,
configurable: true,
writable: true
}))
let arr = [1, 2, 3, 4, 5]
console.log(arr.selfSome(item => item === 2))