Created
February 12, 2014 05:27
-
-
Save cuing/8950473 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 generic methods | |
These are currently not part of ECMAScript standards (though the ES6 Array.from() can be used to achieve this). | |
The following is a shim to allow its use in all browsers: | |
src: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | |
*/ | |
(function () { | |
'use strict'; | |
var i, | |
// We could also build the array of methods with the following, but the | |
// getOwnPropertyNames() method is non-shimable: | |
// Object.getOwnPropertyNames(Array).filter(function (methodName) { | |
// return typeof Array[methodName] === 'function'}); | |
methods = [ | |
'join', 'reverse', 'sort', 'push', 'pop', 'shift', 'unshift', | |
'splice', 'concat', 'slice', 'indexOf', 'lastIndexOf', | |
'forEach', 'map', 'reduce', 'reduceRight', 'filter', | |
'some', 'every' | |
], | |
methodCount = methods.length, | |
assignArrayGeneric = function (methodName) { | |
if (!Array[methodName]) { | |
var method = Array.prototype[methodName]; | |
if (typeof method === 'function') { | |
Array[methodName] = function () { | |
return method.call.apply(method, arguments); | |
}; | |
} | |
} | |
}; | |
for (i = 0; i < methodCount; i++) { | |
assignArrayGeneric(methods[i]); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment