首先出这篇文章,一方面是为了记录巩固我所学的知识,明白面试的高频考点。不鼓励你们背题的,初衷是但愿总结的一些面试题能帮助你查漏补缺,温故知新。这些题并非所有,若是你还想看得更多,能够访问GitHub仓库,目前已经有552道大厂真题了,涵盖各种前端的真题,欢迎加入咱们一块儿来讨论~javascript
Function.prototype.myCall = function (obj, ...args) {
if (obj == undefined || obj == null) {
obj = globalThis
}
obj.fn = this
let res = obj.fn(...args)
delete obj.fn
return res
}
value = 2
let foo = {
value: 1,
}
let bar = function (name, age) {
console.log(name, age, this.value)
}
bar.myCall(foo, 'HearLing', 18) //HearLing 18 1
bar.myCall(null, 'HearLing', 18) //HearLing 18 2
复制代码
Function.prototype.myAplly = function (obj, arr) {
if (obj == undefined || obj == null) {
obj = globalThis
}
obj.fn = this
let res = obj.fn(...arr)
delete obj.fn
return res
}
value = 2
let foo = {
value: 1,
}
let bar = function (name, age) {
console.log(name, age, this.value)
}
bar.myAplly(foo, ['HearLing', 18]) //HearLing 18 1
bar.myAplly(null, ['HearLing', 18]) //HearLing 18 2
复制代码
Function.prototype.myBind = function (obj, ...args) {
let that = this
let fn = function () {
if (this instanceof fn) {
return new that(...args)
} else {
return that.call(obj, ...args)
}
}
return fn
}
value = 2
let foo = {
value: 1,
}
let bar = function (name, age) {
console.log(name, age, this.value)
}
let fn = bar.myBind(foo, 'HearLing', 18)
//fn() //HearLing 18 1
let a = new fn() //HearLing 18 undefined
console.log(a.__proto__)//bar {}
复制代码
call(obj)/apply(obj)::调用函数, 指定函数中的this为第一个参数的值 bind(obj): 返回一个新的函数, 新函数内部会调用原来的函数, 且this为bind()指定的第一参数的值css
function throttle(fn, delay) {
let flag = true
return (...args) => {
if (!flag) return
flag = false
setTimeout(() => {
fn.apply(this, args)
flag = true
}, delay)
}
}
复制代码
function debounce(fn, delay) {
let timer = null
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
复制代码
首先概念上的不一样,解释一下什么是防抖节流;而后就是使用场景的不一样; 经典区分图: 前端
function mycurry(fn, beforeRoundArg = []) {
return function () {
let args = [...beforeRoundArg, ...arguments]
if (args.length < fn.length) {
return mycurry.call(this, fn, args)
} else {
return fn.apply(this, args)
}
}
}
function sum(a, b, c) {
return a + b + c
}
let sumFn = mycurry(sum)
console.log(sumFn(1)(2)(3))//6
复制代码
function unique(arr) {
const res = []
const obj = {}
arr.foreach((item) => {
if (obj[item] === undefined) {
obj[item] = true
res.push(item)
}
})
return res
}
//其余方法
//Array.from(new Set(array))
//[...new Set(array)]
复制代码
// 递归展开
function flattern1(arr) {
let res = []
arr.foreach((item) => {
if (Array.isArray(item)) {
res.push(...flattern1(item))
} else {
res.push(item)
}
})
return res
}
复制代码
function newInstance (Fn, ...args) {
const obj = {}
obj.__proto__ = Fn.prototype
const result = Fn.call(obj, ...args)
// 若是Fn返回的是一个对象类型, 那返回的就再也不是obj, 而是Fn返回的对象不然返回obj
return result instanceof Object ? result : obj
}
复制代码
function instance_of(left, right) {
let prototype = right.prototype
while (true) {
if (left === null) {
return false
} else if (left.__proto__ === prototype) {
return true
}
left = left.__proto__
}
}
let a = {}
console.log(instance_of(a, Object))//true
复制代码
// 浅拷贝的方法
//Object.assign(target,...arr)
// [...arr]
// Array.prototype.slice()
// Array.prototype.concate()
function cloneShallow(origin) {
let target = {}
for (let key in origin) {
if (origin.hasOwnProperty(key)) {
target[key] = origin[key]
}
}
return target
}
let obj = {
name: 'lala',
skill: {
js: 1,
css: 2,
},
}
let newobj = cloneShallow(obj)
newobj.name = 'zhl'
newobj.skill.js = 99
console.log(obj)//{ name: 'lala', skill: { js: 99, css: 2 } }
console.log(newobj)//{ name: 'zhl', skill: { js: 99, css: 2 } }
复制代码
// 浅拷贝的方法
//JSON.parse(JSON.stringify(obj))
function deepClone(source, hashMap = new WeakMap()) {
let target = new source.constructor()
if (source == undefined || typeof source !== 'object') return source
if (source instanceof Date) return source(Date)
if (source instanceof RegExp) return source(RegExp)
hashMap.set(target, source)//解决循环引用
for (let key in source) {
if (source.hasOwnProperty(key)) {
target[key] = deepClone(source[key], hashMap)
}
}
return target
}
let obj = {//应该考虑更复杂的数据
name: 'lala',
skill: {
js: 1,
css: 2,
},
}
let newobj = deepClone(obj)
newobj.skill.js = 100
console.log(obj)//{ name: 'lala', skill: { js: 1, css: 2 } }
console.log(newobj)//{ name: 'lala', skill: { js: 99, css: 2 } }
复制代码
🚀🚀 更多基础知识总结能够⭐️关注我,后续会持续更新面试题总结~java
⭐️⭐️ 最后祝各位正在准备秋招补招和春招的小伙伴面试顺利~,收割offer,咱们一块儿加油吧🤝!还有就是快春节了,提早祝你新年快乐~❤ ❤git