1NetMedia_Blog

<< Math对象取整及四舍五入  类别: JS js常见的内存泄漏及解决方法总汇... >>
几种ES5方法的兼容性封装
[ 日期: 2019-05-15 4:17:08 PM | 作者: admin | 来源: Original | 人气:64 | 晴天 | 心情: +2 ] 字号: 【
急速栏目链接:
标签:兼容  |  es5  |  es6  |  
#indexOf

if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
#filter

if (!Array.prototype.filter) {
Array.prototype.filter = function(fun) {
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
if (fun.call(thisArg, val, i, t))
res.push(val);
}
}
return res;
};
}
#reduce

if ('function' !== typeof Array.prototype.reduce) {
Array.prototype.reduce = function(callback, opt_initialValue) {
'use strict';
if (null === this || 'undefined' === typeof this) {
throw new TypeError(
'Array.prototype.reduce called on null or undefined');
}
if ('function' !== typeof callback) {
throw new TypeError(callback + ' is not a function');
}
var index, value,
length = this.length >>> 0,
isValueSet = false;
if (1 < arguments.length) {
value = opt_initialValue;
isValueSet = true;
}
for (index = 0; length > index; ++index) {
if (this.hasOwnProperty(index)) {
if (isValueSet) {
value = callback(value, this[index], index, this);
} else {
value = this[index];
isValueSet = true;
}
}
}
if (!isValueSet) {
throw new TypeError('Reduce of empty array with no initial value');
}
return value;
};
}

#Array.prototype.forEach()

if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
exception.
if(typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = thisArg;
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
#String.prototype.trim()

if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
#Promise
http://www.mamicode.com/info-detail-2670179.html

function ResolutionRetrunPromise (nextPromise, returnValue, res, rej) {
// 返回值是否是promise
if (returnValue instanceof myPromise) {
returnValue.then(function (val) {
res(val)
},function (reason) {
rej(reason)
})
} else {
res(returnValue)
}
}
myPromise.prototype.then = function (onFulfilled, onRejected) {
// 参数为空把值直接传给下一个then
if (!onFulfilled) {
onFulfilled = function (val) {
return val;
}
}
if (!onRejected) {
onRejected = function (reason) {
return new Error(reason)
}
}
var _this = this;
// 用于链式调用
var nextPromise = new myPromise(function(res, rej) {
if (_this.status == 'Fulfilled') {
// 存一下回调执行的结果,传给下一个.then
setTimeout(function () {
// 捕获错误
try {
var nextResolveValue = onFulfilled(_this.resolveValue);
// 处理返回值
ResolutionRetrunPromise(nextPromise, nextResolveValue, res, rej)
// res(nextResolveValue);
} catch (e) {
rej(e)
}

},0)
}
if (_this.status == 'Rejected') {
// 存一下回调执行的结果,传给下一个.then
setTimeout(function () {
// 捕获错误
try {
var nextRejectValue = onRejected(_this.rejectValue);
ResolutionRetrunPromise(nextPromise, nextRejectValue, res, rej)
// rej(nextRejectValue);
} catch (e) {
rej(e)
}
},0)
}
// 等待状态时把回调存起来,状态改变再触发
if (_this.status == 'pending') {
_this.resolveCallbackList.push(function () {
// 存一下回调执行的结果,传给下一个.then
setTimeout(function () {
// 捕获错误
try {
var nextResolveValue = onFulfilled(_this.resolveValue);
ResolutionRetrunPromise(nextPromise, nextResolveValue, res, rej)
// res(nextResolveValue);
} catch (e) {
rej(e)
}
},0)
});
_this.rejectCallbackList.push(function () {
setTimeout(function () {
// 捕获错误
try {
var nextRejectValue = onRejected(_this.rejectValue);
ResolutionRetrunPromise(nextPromise, nextRejectValue, res, rej)
// rej(nextRejectValue);
} catch (e) {
rej(e)
}
},0)
});
}
});
return nextPromise
};
测试:

var p = new myPromise((resolve, reject) => {
setTimeout(() => {
resolve('I am handsome');
}, 1000);
});
p.then((data) => {
console.log(data + ' suc' + ' 1');
return new myPromise((resolve, reject) => {
reject('promise')
})
}, (err) => {
console.log(err + ' err' + ' 1')
} ).then((data) => {
console.log(data + ' suc' + ' 3')
}, (err) => {
console.log(err + ' err' + ' 3')
} );

   
Rss Comment  

没有相关评论

发表评论
请先注册,才能发表评论!
- 注册 -