Created
September 4, 2019 02:45
-
-
Save shekhardtu/a3c9be0ead9f0b20cae268709c32e8ed to your computer and use it in GitHub Desktop.
polyfill of every array method in javascript [ES6]
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.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