实现一个new操作符
function myNew ( fn, ... args ) { if ( typeof fn !== 'function' ) { throw ( 'fn is not a function' ) } let res= Object. create ( fn. prototype) let result= fn . apply ( res, args) return result
}
实现instanceof
function myInstanceof ( left, right ) { if ( typeof left !== 'object' || left === null ) return false let proto= Object. getPrototypeOf ( left) while ( true ) { if ( proto== null ) return false if ( proto== right. prototype) return true proto= Object. getPrototypeOf ( proto) }
}
实现浅拷贝
function shallowClone ( obj ) { if ( typeof obj !== 'object' || obj== null ) return objlet newObj= Array. isArray ( obj) ? [ ] : { } for ( let key in obj) { newObj[ key] = obj[ key] } return newObj
}
实现深拷贝
function deepClone ( obj ) { let cloneObj= Array. isArray ( obj) ? [ ] : { } ; for ( let key in obj) { if ( typeof obj[ key] === 'object' ) { cloneObj[ key] = deepClone ( obj[ key] ) ; } else { cloneObj[ key] = obj[ key] ; } } return cloneObj
}
实现call/apply
Function . prototype. myCall = function ( val, ... args ) { if ( typeof this !== 'function' ) { throw new TypeError ( 'error' ) } let context= val || windowcontext. fn = this ; const result = context. fn ( ... args) ; delete context. fn; return result;
}
实现防抖
function debounce ( fn, delay ) { let timer = null ; return function ( ) { if ( timer) clearTimeout ( timer) ; timer= setTimeout ( fn, delay) }
}
实现节流
function throttle ( fn, delay ) { let valid= true ; return function ( ) { if ( ! valid) return ; setTimeout ( ( ) => { fn ( ) ; valid= true ; } , delay) }
}
实现promise
class MyPromise { static PENDING = 'pending' ; static FULFILLED = 'fulfilled' ; static REJECTED = 'rejected' ; constructor ( fn ) { this . status= MyPromise. PENDING ; this . result= null try { fn ( this . resolve . bind ( this ) , this . reject . bind ( this ) ) } catch ( err) { this . reject ( err) } } resolve ( result ) { if ( this . status=== MyPromise. PENDING ) { this . status= MyPromise. FULFILLED this . result= result} } reject ( result ) { if ( this . status=== MyPromise. PENDING ) { this . status= MyPromise. REJECTED this . result= result} } then ( resolve, reject ) { resolve= typeof resolve=== 'function' ? resolve : ( ) => { } reject= typeof reject=== 'function' ? reject : ( ) => { } if ( this . status=== MyPromise. FULFILLED ) { setTimeout ( ( ) => { resolve ( this . result) } ) } if ( this . status=== MyPromise. REJECTED ) { setTimeout ( ( ) => { reject ( this . result) } ) } }
}
实现函数柯里化
function curry ( fn, args ) { var num= fn. lengthvar args= args|| [ ] ; return function ( ) { var arr= [ ] . slice . call ( arguments) ; [ ] . push . apply ( arr, args) if ( arr. length< num) { return curry . call ( this , fn, arr) } return fn . apply ( this , arr) }
}
实现无限级目录树
function renderMenu ( menuItems, parentElement ) { const ul= document. createElement ( "ul" ) ; menuItems. forEach ( item => { const li= document. createElement ( "li" ) ; li. textContent= item. name; if ( item. children && item. children. length> 0 ) { renderMenu ( item. children, li) ; } ul. appendChild ( li) ; } ) if ( parentElement) { parentElement. appendChild ( ul) ; }
}
实现快速排序
function quickSort ( arr ) { if ( arr. length<= 1 ) return arrlet base= arr[ 0 ] let leftArr= [ ] let rightArr= [ ] for ( let i= 1 ; i< arr. length; i++ ) { if ( arr[ i] < base) leftArr. push ( arr[ i] ) else rightArr. push ( arr[ i] ) } let resArr= quickSort ( leftArr) . concat ( base, quickSort ( rightArr) ) return resArr
}