ie的存在可謂是程序員的噩夢,但更噩夢的面對堅持使用它的那些有錢有權沒技術的客戶.
兼容補丁用於js開頭,如:程序員
//兼容補丁 Array.prototype.filter=.... function myfunction(){ array.filter....//you script }
經常使用:npm
//兼容indexOf Array.prototype.indexOf||(Array.prototype.indexOf=function(b,c){for(var a=c||0,d=this.length;a<d;a++)if(this[a]===b)return a;return-1}); /*兼容console*/ window.console=window.console||function(){var a={};a.log=a.warn=a.debug=a.info=a.error=a.time=a.dir=a.profile=a.clear=a.exception=a.trace=a.assert=function(){};return a}(); //兼容filter Array.prototype.filter||(Array.prototype.filter=function(c,f){if(void 0===this||null===this)throw new TypeError;var b=Object(this),g=b.length>>>0;if("function"!=typeof c)throw new TypeError;for(var d=[],a=0;a<g;a++)if(a in b){var e=b[a];c.call(f,e,a,b)&&d.push(e)}return d}); //兼容bind Function.prototype.bind=Function.prototype.bind||function(b){if("function"!==typeof this)throw new TypeError("bind\u4ec5\u7528\u4e8efunction");var d=Array.prototype.slice.call(arguments,1),e=this,a=function(){},c=function(){return e.apply(this instanceof a&&b?this:b,d.concat(Array.prototype.slice.call(arguments)))};a.prototype=this.prototype;c.prototype=new a;return c}; //兼容reduce "function"!==typeof Array.prototype.reduce&&(Array.prototype.reduce=function(d,e){if(null===this||"undefined"===typeof this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!==typeof d)throw new TypeError(d+" is not a function");var a=0,f=this.length>>>0,b,c=!1;1<arguments.length&&(b=e,c=!0);for(;f>a;++a)this.hasOwnProperty(a)&&(c?b=d(b,this[a],a,this):(b=this[a],c=!0));if(!c)throw new TypeError("Reduce of empty array with no initial value");return b}); //兼容Promise /*https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js*/ !function(l,h){"object"==typeof exports&&"undefined"!=typeof module?h():"function"==typeof define&&define.amd?define(h):h()}(0,function(){function l(a){var b=this.constructor;return this.then(function(c){return b.resolve(a()).then(function(){return c})},function(c){return b.resolve(a()).then(function(){return b.reject(c)})})}function h(){}function d(a){if(!(this instanceof d))throw new TypeError("\u5fc5\u987b\u7528new\u6784\u5efa");if("function"!=typeof a)throw new TypeError("not a function");this._state=0;this._handled=!1;this._value=void 0;this._deferreds=[];p(a,this)}function q(a,b){for(;3===a._state;)a=a._value;0!==a._state?(a._handled=!0,d._immediateFn(function(){var c=1===a._state?b.onFulfilled:b.onRejected;if(null!==c){var f;try{f=c(a._value)}catch(g){return void e(b.promise,g)}m(b.promise,f)}else(1===a._state?m:e)(b.promise,a._value)})):a._deferreds.push(b)}function m(a,b){try{if(b===a)throw new TypeError("\u4e0d\u80fdresolve\u8be5Promise\u81ea\u8eab");if(b&&("object"==typeof b||"function"==typeof b)){var c=b.then;if(b instanceof d)return a._state=3,a._value=b,void n(a);if("function"==typeof c)return void p((f=c,g=b,function(){f.apply(g,arguments)}),a)}a._state=1;a._value=b;n(a)}catch(r){e(a,r)}var f,g}function e(a,b){a._state=2;a._value=b;n(a)}function n(a){2===a._state&&0===a._deferreds.length&&d._immediateFn(function(){a._handled||d._unhandledRejectionFn(a._value)});for(var b=0,c=a._deferreds.length;c>b;b++)q(a,a._deferreds[b]);a._deferreds=null}function p(a,b){var c=!1;try{a(function(a){c||(c=!0,m(b,a))},function(a){c||(c=!0,e(b,a))})}catch(f){c||(c=!0,e(b,f))}}var t=setTimeout;d.prototype["catch"]=function(a){return this.then(null,a)};d.prototype.then=function(a,b){var c=new this.constructor(h);return q(this,new function(a,b,c){this.onFulfilled="function"==typeof a?a:null;this.onRejected="function"==typeof b?b:null;this.promise=c}(a,b,c)),c};d.prototype["finally"]=l;d.all=function(a){return new d(function(b,c){function d(a,f){try{if(f&&("object"==typeof f||"function"==typeof f)){var e=f.then;if("function"==typeof e)return void e.call(f,function(b){d(a,b)},c)}g[a]=f;0==--h&&b(g)}catch(u){c(u)}}if(!a||void 0===a.length)throw new TypeError("all\u4e2d\u5fc5\u987b\u662f\u6570\u7ec4");var g=Array.prototype.slice.call(a);if(0===g.length)return b([]);for(var h=g.length,e=0;g.length>e;e++)d(e,g[e])})};d.resolve=function(a){return a&&"object"==typeof a&&a.constructor===d?a:new d(function(b){b(a)})};d.reject=function(a){return new d(function(b,c){c(a)})};d.race=function(a){return new d(function(b,c){for(var d=0,e=a.length;e>d;d++)a[d].then(b,c)})};d._immediateFn="function"==typeof setImmediate&&function(a){setImmediate(a)}||function(a){t(a,0)};d._unhandledRejectionFn=function(a){void 0!==console&&console&&console.warn("\u672a\u8bbe\u7f6ereject:",a)};var k=function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw Error("\u627e\u4e0d\u5230\u5168\u5c40\u5bf9\u8c61");}();"Promise"in k?k.Promise.prototype["finally"]||(k.Promise.prototype["finally"]=l):k.Promise=d});