Pro's and Con's of Polyfilling this way?

Hi guys,

I need to add two polyfills to a site I’m working on so that it has support for older browsers. I can’t use a third party like polyfill.io or anything. I have created this solution which runs on page load and works fine, but wondering if it could / should be improved?

Thanks in advance! :slight_smile:

(function loadPolyfills() {
	var browserFeatures = [
		{
			polyfill: (function matchesPolyfill(){return Element.prototype.matches||(Element.prototype.matches=Element.prototype.matchesSelector||Element.prototype.mozMatchesSelector||Element.prototype.msMatchesSelector||Element.prototype.oMatchesSelector||Element.prototype.webkitMatchesSelector||function(e){for(var t=(this.document||this.ownerDocument).querySelectorAll(e),o=t.length;--o>=0&&t.item(o)!==this;);return o>-1})})(),
			detected: Element.prototype.matches !== undefined
		},
		{
			polyfill: (function promisePolyfill(){return e=this,t=function(){"use strict";function e(){}function t(e){if(!(this instanceof t))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(e,this)}function n(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,t._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var i;try{i=t(e._value)}catch(e){return void r(n.promise,e)}o(n.promise,i)}else(1===e._state?o:r)(n.promise,e._value)})):e._deferreds.push(n)}function o(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var o=n.then;if(n instanceof t)return e._state=3,e._value=n,void i(e);if("function"==typeof o)return void f((u=o,c=n,function(){u.apply(c,arguments)}),e)}e._state=1,e._value=n,i(e)}catch(t){r(e,t)}var u,c}function r(e,t){e._state=2,e._value=t,i(e)}function i(e){2===e._state&&0===e._deferreds.length&&t._immediateFn(function(){e._handled||t._unhandledRejectionFn(e._value)});for(var o=0,r=e._deferreds.length;r>o;o++)n(e,e._deferreds[o]);e._deferreds=null}function f(e,t){var n=!1;try{e(function(e){n||(n=!0,o(t,e))},function(e){n||(n=!0,r(t,e))})}catch(e){if(n)return;n=!0,r(t,e)}}var u=setTimeout;return t.prototype.catch=function(e){return this.then(null,e)},t.prototype.then=function(t,o){var r=new this.constructor(e);return n(this,new function(e,t,n){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof t?t:null,this.promise=n}(t,o,r)),r},t.prototype.finally=function(e){var t=this.constructor;return this.then(function(n){return t.resolve(e()).then(function(){return n})},function(n){return t.resolve(e()).then(function(){return t.reject(n)})})},t.all=function(e){return new t(function(t,n){function o(e,f){try{if(f&&("object"==typeof f||"function"==typeof f)){var u=f.then;if("function"==typeof u)return void u.call(f,function(t){o(e,t)},n)}r[e]=f,0==--i&&t(r)}catch(e){n(e)}}if(!e||void 0===e.length)throw new TypeError("Promise.all accepts an array");var r=Array.prototype.slice.call(e);if(0===r.length)return t([]);for(var i=r.length,f=0;r.length>f;f++)o(f,r[f])})},t.resolve=function(e){return e&&"object"==typeof e&&e.constructor===t?e:new t(function(t){t(e)})},t.reject=function(e){return new t(function(t,n){n(e)})},t.race=function(e){return new t(function(t,n){for(var o=0,r=e.length;r>o;o++)e[o].then(t,n)})},t._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){u(e,0)},t._unhandledRejectionFn=function(e){void 0!==console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)},t},!void("object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Promise=t());var e,t})(),
			detected: window.Promise !== undefined
		}
	]

	browserFeatures.forEach(function (feature) {
		return !feature.detected && feature.polyfill
	})
})();