Skip to content

Instantly share code, notes, and snippets.

@shekhardtu
Created September 4, 2019 02:45
Show Gist options
  • Save shekhardtu/a3c9be0ead9f0b20cae268709c32e8ed to your computer and use it in GitHub Desktop.
Save shekhardtu/a3c9be0ead9f0b20cae268709c32e8ed to your computer and use it in GitHub Desktop.
polyfill of every array method in javascript [ES6]
Array.prototype.myEvery = 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 false;
}
}
return true;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var every = arr.every((item)=>item > 2);
var myEvery = arr.myEvery((item)=>item > 2);
console.log(every); // false
console.log(myEvery); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment