You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionTimeoutPromise(promise,ms){constdelayPromise=ms=>{returnnewPromise(function(resolve){setTimeout(resolve,ms)})}consttimeout=delayPromise(ms).then(function(){thrownewError('operation time out after '+ms+' ms')})returnPromise.race([promise,timeout])}functiontestPromise(){returnnewPromise((resolve,reject)=>{setTimeout(()=>{resolve()console.log('resolve emit')},3000)})}TimeoutPromise(testPromise(),5000)
functionpromiseAll(promises){returnnewPromise(function(resolve,reject){if(!Array.isArray(promises)){returnreject(newTypeError('arguments must be an array'))}letresolvedCounter=0letpromiseNum=promises.lengthletresolvedValues=newArray(promiseNum)for(leti=0;i<promiseNum;i++){Promise.resolve(promises[i]).then(function(value){resolvedCounter++resolvedValues[i]=valueif(resolvedCounter===promiseNum){returnresolve(resolvedValues)}},function(reason){returnreject(reason)})}})}
原理: L 的 proto 是不是等于 R.prototype,不等于再找 L.proto.proto 直到 proto 为 null
functioninstance_of(L,R){varO=R.prototype;L=L.__proto__;while(true){if(L===null)returnfalse;// 这里重点:当 O 严格等于 L 时,返回 trueif(O===L)returntrue;L=L.__proto__;}}
Function.prototype.bind2=function(context){if(typeofthis!=="function"){thrownewError("Function.prototype.bind - what is trying to be bound is not callable");}varself=this;varargs=Array.prototype.slice.call(arguments,1);varfNOP=function(){};varfBound=function(){varbindArgs=Array.prototype.slice.call(arguments);returnself.apply(thisinstanceoffNOP ? this : context,args.concat(bindArgs));}fNOP.prototype=this.prototype;fBound.prototype=newfNOP();returnfBound;}
The text was updated successfully, but these errors were encountered:
参考文献
实现双向绑定
JSON.parse
defineProperty 实现
proxy 实现
防抖
节流
async/await 实现
使用 generator 函数实现
实现promise.race
promise.race 实现超时函数
使用 promise 函数实现
实现 promise.all
实现promise.race
实现promise.retry
实现 promise
参考地址
柯里化
Object.create 实现原理
instanceof 实现
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
splice 实现
new 操作的实现
单例实现
call
apply
bind
The text was updated successfully, but these errors were encountered: