// promise.all
function promiseAll(promises) {
let proArr = Array.from(promises)
let proLen = proArr.length
let resList = []
return new Promise((resolve,reject) => {
for(let i = 0; i < proLen; i++) {
Promise.resolve(proArr[i]).then(res => {
resList.push(res)
if(resList.length == proLen) resolve(resList)
}).catch(e => {
reject(e)
})
}
})
}
// promise.race
function promiseRace(promises) {
let proArr = Array.from(promises)
let proLen = proArr.length
return new Promise((resolve,reject) => {
for(let i = 0; i < proLen; i++) {
Promise.resolve(proArr[i])
.then(res => {
resolve(res)
})
.catch(e => {
reject(e)
})
}
})
}
// new
function New(fn) {
// let obj = {}
// obj.__proto__ = fn.prototype
let obj = Object.create(fn.prototype)
let args = [].slice.call(arguments,1)
fn.apply(obj,args)
return obj
}
// instanceof
function Instanceof(a,b) {
let prototype = b.prototype
let proto= a.__proto__
while(proto){
if(proto=== prototype) return true
proto = proto.__proto__
}
return false
}
// deep copy
function deepCopy(obj) {
if(obj === null) return obj
if(typeof obj !== 'object') return obj
if(obj instanceof Date) return new Date(obj)
if(obj instanceof RegExp) return new RegExp(obj)
let cloneObj = new obj.constructor()
for(let key in obj){
if(obj.hasOwnProperty(key)) {
cloneObj[key] = typeof obj[key] === 'object' ? DeepCopy(obj[key]) : obj[key]
}
}
return cloneObj
}
// call、apply、bind
Function.prototype.myCall = function(context,...args) {
context = context ? context : window
context.fn = this
let result = context.fn(...args)
delete context.fn
return result
}
Function.prototype.myApply = function(context,args) {
context = context ? context : window
context.fn = this
let result = args && args.length ? context.fn(...args) : context.fn()
delete context.fn
return result
}
Function.prototype.myBind = function(context,...args) {
context = context ? context : window
context.fn = this
return function() {
let newArgs = args.concat(...arguments)
let result = context.fn(...newArgs)
delete context.fn
return result
}
}
// debounce
function debounce(fn,delay) {
let timer = null
return function() {
timer && clearTimeout(timer)
timer = setTimeout(() => {
fn()
},delay)
}
}
// throttle
function throttle(fn,delay) {
let timer = true
return function() {
if(!timer) return
timer = false
setTimeout(() => {
fn()
timer = true
},delay)
}
}
// jsonP
function jsonP(link,cb){
let script = document.createElement('script')
let funcName = Symbol()
script.src = link + `?cb=${funcName}`
document.getElementsByTagName('head')[0].appendChild(script)
window[funcName] = function(data) {
cb(data)
}
window[funcName]()
delete window[funcName]
}
// Object.create
function create(obj){
function fn(){}
fn.prototype = obj
fn.prototype.costructor = fn
return new fn()
}
// 继承
function Parent(name) {
this.name = name
}
Parent.prototype.sayName = function() {
console.log(this.name)
}
function Son(name){
Parent.call(this,name)
}
// 组合继承
Son.prototype = new Parent()
// 寄生组合继承
Son.prototype = Object.create(Parent.prototype)
Son.prototype.constructor = Son
// ES6
class Son extends Parent {
constructor(name){
super(name) // 相当于 Parent.call(this,name)
}
}
// ajax
function myAjax(url,method='GET',data=null) {
let xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
data = data ? queryString(data) : data
if(method == 'GET') {
url = data ? url + '?' + data : url
xhr.open(url,method,true)
xhr.send()
} else if(method == 'POST') {
xhr.open(url,method,true)
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded')
xhr.send(data)
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
console.log(xhr.responseText)
}
}
}
function queryString(data) {
let res = ''
for(let key in data) {
res += key + '=' + data[key] + '&'
}
return res.substring(0,res.length - 1)
}
}
// curry
function curry(fn,...args1) {
if(args1.length >= fn.length) return fn(...args1)
return function(...args2) {
return curry(fn,...args1,...args2)
}
}
// trim
function Trim(str) {
let len = str.length
let strArr = str.split('')
let startFlag = false
for(let i = 0; i < len; i++) {
if(strArr[i] == ' ' && !startFlag) {
strArr[i] = ''
} else {
startFlag = true
}
}
startFlag = false
for(let i = strArr.length - 1; i >= 0; i--) {
if(strArr[i] == ' ' && !startFlag) {
strArr[i] = ''
} else {
startFlag = true
}
}
return strArr.join('')
}
手写Promise.all、new、instanceof、深拷贝、call/apply/bind、防抖/节流、JSONP、Object.create、继承、ajax
最新推荐文章于 2024-05-23 14:28:42 发布