ie低版本常用js兼容库

ie的存在可谓是程序员的噩梦,但更噩梦的面对坚持使用它的那些有钱有权没技术的客户.
兼容补丁用于js开头,如:

//兼容补丁
Array.prototype.filter=....
function myfunction(){
array.filter....//you script
}

常用:

//兼容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});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值