1. 手写new
function myNew(fn, ...args) {
let obj = {}
obj.__proto__ = fn.prototype
let res = fn.apply(obj, args)
return res instanceof Object ? res : obj
}
2. 手写instanceOf
function myInstanceOf(instance, target) {
if (!instance || !target) {
return
}
if (typeof target !== 'function') {
throw Error('target不是一个构造函数!')
}
let proto = Object.getPrototypeOf(instance)
let targetProto = target.prototype
while (true) {
if (!proto) return false
if (proto === targetProto) return true
proto = proto.getPrototypeOf(proto)
}
}
3. 手写深拷贝
function deepClone(obj) {
if (typeof obj === 'object') {
const temp = Array.isArray(obj) ? [] : {}
for (let k in obj) {
temp[k] = deepClone(obj[k])
}
return temp
} else {
return obj
}
}
4. 手写apply、call、bind
let obj = { name: 'zhangsan' }
function testFn(age, job) {
console.log(this.name)
console.log(age)
console.log(job)
}
Function.prototype.myCall = function (context, ...args) {
context = context || window
context.fn = this
const result = context.fn(...args)
delete context.fn
return result
}
testFn.myCall(obj, '18', '攻城狮')
Function.prototype.myApply = function (context, argsArray) {
context = context || window
context.fn = this
const result = context.fn(...argsArray)
delete context.fn
return result
}
testFn.myApply(obj, ['18', '攻城狮'])
Function.prototype.myBind = function (context, ...args) {
const self = this
return function (...args2) {
return self.apply(context, [...args, ...args2])
}
}
let res = testFn.bind(obj, '18', '攻城狮')
console.log(res())
5. 手写fliter、find函数
let arr = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]
let r1 = arr.filter((item, index) => {
return item.id === 1
})
function Filter(arr, callback) {
let res = []
arr.forEach((item, index) => {
let temp = callback(item, index)
if (temp) {
res.push(temp)
}
})
return res
}
let r = Filter(arr, (item, index) => item.id > 1)
arr.find((item) => item.id === 1)
function Find(arr, callback) {
for (let i = 0; i < arr.length; i++) {
let res = callback(arr[i], i)
if (res) {
return arr[i]
}
}
}
let f = Find(arr, (item, index) => item.id === 2)
6. 数组去重
let arrObj = [
{ name: '小红', id: 1 },
{ name: '小黄', id: 4 },
{ name: '小绿', id: 3 },
{ name: '小青', id: 1 },
{ name: '小蓝', id: 4 }
]
function repeat1(arr) {
let res = []
let obj = {}
for (let k in arr) {
if (!obj[arr[k].id]) {
res.push(arr[k])
obj[arr[k].id] = true
}
}
return res
}
console.log(repeat1(arrObj))
function repeat2(arr) {
let map = new Map()
for (let item of arr) {
if (!map.has(item.id)) {
map.set(item.id, item)
}
}
return [...map.values()]
}
console.log(repeat2(arrObj))
function repeat3(arr) {
let temp = []
arr.forEach((item, index) => {
temp.push(item.id)
if (temp.includes(item.id)) {
arr.splice(index, 1)
}
})
return arr
}
console.log(repeat3(arrObj))
7. 防抖和节流
function debounce(fn, delay = 1000) {
let timer = null
return function (...args) {
console.log(timer)
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, delay)
}
}
function throttle(fn, delay = 1000) {
let timer = null
return function (...args) {
if (timer) return
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, delay)
}
}
function throttle(fn, delay = 1000) {
let lastTime = 0
return function (...args) {
let now = Date.now()
if (now - lastTime >= delay) {
fn.apply(this, args)
lastTime = now
}
}
}