31、Promise手写
<script>
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
class myPromise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = [];
this.onRejectedCallbacks = [];
let resolve = (value) => {
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
this.onResolvedCallbacks.forEach((fn) => fn());
}
};
let reject = (reason) => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
this.onRejectedCallbacks.forEach((fn) => fn());
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === "function" ? onFulfilled : (v) => v;
onRejected = typeof onRejected === "function" ? onRejected : (err) => {
throw err;
};
let promise2 = new Promise((resolve, reject) => {
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
}
if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
}
if (this.status === PENDING) {
this.onResolvedCallbacks.push(() => {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
}
});
return promise2;
}
}
const resolvePromise = (promise2, x, resolve, reject) => {
if (promise2 === x) {
return reject(
new TypeError("Chaining cycle detected for promise #<Promise>"));
}
let called;
if ((typeof x === "object" && x != null) || typeof x === "function") {
try {
let then = x.then;
if (typeof then === "function") {
then.call(
x, (y) => {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject);
}, (r) => {
if (called) return;
called = true;
reject(r);
});
} else {
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
} else {
resolve(x);
}
};
debugger
const promise = new myPromise((resolve, reject) => {
resolve('true');
reject('error')
})
promise.then(1);
debugger
</script>
32、filter函数
<script>
Array.prototype.myFilter = function (callback, thisArg) {
if (this == null) {
throw new TypeError('this is null or undefined')
}
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
const O = Object(this);
const len = O.length >>> 0;
let k = 0, res = [];
while (k < len) {
if (k in O) {
if (callback.call(thisArg, O[k], k, O)) {
res.push(O[k]);
}
}
k++;
}
return res;
}
const arr = [2, 4, 5];
const newArr = arr.myFilter((item) => {
return item !== 4;
});
console.log(newArr)
var person = {
minAge: 18,
checkAge: function(age) {
return age >= this.minAge;
}
};
var ages = [16, 21, 15, 30, 18];
var adults = ages.filter(person.checkAge, person);
console.log(adults);
</script>
33、some函数
<script>
Array.prototype.mySome = function(callback, thisArg) {
if(this === null) {
throw new TypeError('this is null or not defined')
}
if(typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
const O = Object(this);
const len = O.length >>> 0;
let k = 0;
while(k < len) {
if(k in O) {
if(callback.call(thisArg,O[k],k,O)){
return true;
}
}
k++;
}
return false;
}
const arr = [2,4,5];
const newArr = arr.mySome((item) => {
return item === 4;
});
console.log(newArr)
</script>
34、reduce函数
<script>
Array.prototype.myReduce = function(callback, initValue) {
if(this === null) {
throw new TypeError('this is null or not defined')
}
if(typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
const O = Object(this);
const len = O.length >>> 0;
let k = 0,acc;
debugger
if(arguments.length > 1) {
acc = initValue
} else {
while(k < len && !(k in O)) {
k++;
}
if(k > len) {
throw new TypeError('Reduce of empty array with no initial value')
}
acc = O[k++];
}
while(k < len) {
if(k in O) {
acc = callback(acc,O[k],k,O)
}
k++
}
return acc;
}
const arr = ['',4,5,3,6];
const newArr = arr.myReduce((a,b) => {
return a + b;
});
console.log(newArr)
</script>
35、map函数
<script>
Array.prototype.myMap = function (callback, thisArg) {
if (this === null) {
throw new TypeError('this is null or not defined')
}
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function')
}
debugger
const O = Object(this);
const len = O.length >>> 0;
let k = 0, res = [];
while (k < len) {
if (k in O) {
res[k] = callback.call(thisArg, O[k], k, O);
}
k++;
}
return res;
}
const arr = [2, 4, 5];
const newArr = arr.myMap((item) => {
return item + 10;
});
console.log(newArr)
</script