面试题总结
- 不修改下面的代码进行正常解构
let [a,b] = {a:1,b:2}
Object.prototype[Symbol.iterator] = function* (){
// return Object.values(this)[Symbol.iterator]()
return yeild* Object.values(this)
}
ler [a,b] = {a:1,b:2}
- 这行代码打印出什么?console.log(console.log.apply.call.call.call.apply((a) => a , [3,15]))
- 游离节点造成的内存泄漏问题总结
- 惰性函数
let flag = false
function getTableList(){
if(flag){
getTableList = () => {
return `我变成了第一个函数${flag}`
}
// 第一次调用
console.log(getTableList())
}else{
getTableList = () => {
return `我变成了第二个函数${flag}`
}
// 第一次调用
console.log(getTableList())
}
}
console.log( getTableList())

let flag = false
function getTableList(){
if(flag){
return () => {
return `我变成了第一个函数${flag}`
}
}else{
return () => {
return `我变成了第二个函数${flag}`
}
}
}
let getTable = getTableList()
console.log( getTable())
- 写一个防抖函数并大致说出它的原理
function debounce(fn,duration = 300){
// 判断传入的是一个函数表达式
if(typeof fn != 'function'){
throw new TypeError ('请传入函数')
}
//利用闭包的原理实现定时器的清空
let timer = null
return function(...args){
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this,args)
},duration)
}
}
- 输出下面表达式的值
([][[]]+[])[+!![]]+([]+{})[+!![]+!!{}]
- 不适用for循环之类的方法将一个数组求和
let arr = [1,2,3,4,5,6,7,8,9,10,11]
function sum(arr){
let i = 0;
let sum = 0;
function _fun(){
if(i >= arr.length){
return
}
sum += arr[i]
i++
_fun()
}
_fun()
return sum
}
console.log(sum(arr))
// 第二种写法
let sum = 0;
function getSum (arr,index = 0){
if(index >= arr.length){
return
}
sum += arr[index]
index ++
getSum(arr,index)
return sum
}
- 写出一下表达式的值
"use strict";
letAPI;
(function (API) {
API[API["user"] = 0] = "user";
API[API["getAge"] = 1] = "getAge";
})(API || (API = {}));
console.log(API) // {0: 'user', 1: 'getAge', user: 0, getAge: 1}
- vue中的hooks是什么
import { ElMessage, ElMessageBox } from 'element-plus';
import { i18n } from '../i18n';
const { t } = i18n.global;
interface MessageImplements {
info(title: string): void;
wraning(title: string): void;
success(title: string): void;
error(title: string): void;
}
export function useMessage() {
class MessageClass implements MessageImplements {
// 普通提示
info(title: string): void {
ElMessage.info(title);
}
// 警告提示
wraning(title: string): void {
ElMessage.warning(title);
}
// 成功提示
success(title: string): void {
ElMessage.success(title);
}
// 错误提示
error(title: string): void {
ElMessage.error(title);
}
}
return new MessageClass();
}
//用法
import { useMessage, useMessageBox } from '/@/hooks/message';
// 其中t() 是国际化配置函数
useMessage().success(t('common.delSuccessText'));