5 ms·
Why not just polyfill `Array.prototype.includes`? That's what my project does when the only features we care about missing from an old release are polyfillable.
by Zarel 9y ago
Why not just polyfill `Array.prototype.includes`? That's what my project does when the only features we care about missing from an old release are polyfillable.
Object.defineProperty(Array.prototype, 'includes', { // eslint-disable-line no-extend-native
writable: true, configurable: true,
value: function (object) {
return this.indexOf(object) !== -1;
},
});
Or if you're sure you and your libraries don't use any bad coding practices, you could even do:
Array.prototype.includes = function (object) { // eslint-disable-line no-extend-native
return this.indexOf(object) !== -1;
}
- alangpierce 9y agoThat's what I'd do in an application, but it's bad manners to modify the global Array.prototype from a library, or to expect your parent application to provide such a polyfill, so usually it's best to just avoid polyfills in open source libraries.