Skip to content

Instantly share code, notes, and snippets.

@shekhardtu
Created September 4, 2019 02:43
Show Gist options
  • Save shekhardtu/355acf3c8e5847c7057a78d3d22c8d1b to your computer and use it in GitHub Desktop.
Save shekhardtu/355acf3c8e5847c7057a78d3d22c8d1b to your computer and use it in GitHub Desktop.
polyfill of some array method in javascript
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