Created
September 4, 2019 02:43
-
-
Save shekhardtu/355acf3c8e5847c7057a78d3d22c8d1b to your computer and use it in GitHub Desktop.
polyfill of some array method in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.mySome = function() { | |
var arr = this; | |
var len = arr.length; | |
var fn = arguments[0]; | |
var thisArg = arguments[1]; | |
for (var i = 0; i < len; i++) { | |
if(fn.call(thisArg, arr[i], i, arr)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
var arr = [1, 2, 3, 4, 5, 6, 7, 8]; | |
var some = arr.some((item)=>item > 2); | |
var mySome = arr.mySome((item)=>item > 2); | |
console.log(some); // true | |
console.log(mySome); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment