面试题

b是多少?是直接就报错了。

1
2
3
4
5
function a(){
let b;
console.log(b);
var b=123;
}

问数字是否有toString()方法?

1
2
3
4
5
(123).toString() // "123"
(function(){}).toString() // "function(){}"
true.toString() // "true"
[1,2,{}].toString().toString() // "1,2,[object Object]"
({}).toString() // "[object Object]"

一维数组不含对象去重

1
Array.from(new Set(arr))

json深拷贝

1
2
lodash.cloneDeep(json)
JSON.parse(JSON.stringify(json))

两个一维数组包含json,根据id去重

1
2
3
4
5
6
7
8
9
var a=[{id:1,val:45},{id:2,val:57},{id:3,val:32}]
var b=[{id:3,val:32},{id:4,val:46463},{id:5,val:764123}]
while(b.length){
var p=b.shift()
var t=a.filter(o=>o.id===p.id)
if(!t.length){
a.push(p)
}
}

vue父子互相调用方法

1
2
3
4
// 父组件调用子组件
ref.methodHandle()
// 子组件调用父组件
emit()

vue直接赋值不生效?

1
2
3
4
// 不能直接在页面更新出来
this.form.optnCode = item.optnCode
// 使用这种实时更新数据和页面
this.$set(this.form, 'optnCode', item.optnCode)

有多个异步请求,如何在请求结束后,调用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
// 以下为临时想到的解决方案,如果有遇到更好的解决方案会补充
let flag=0;
function cb(){
flag++
if(flag===5){
console.log('异步请求结束')
}
}
axios.get('/api').then(function(){cb()}).catch(function(){cb()})
axios.get('/api').then(function(){cb()}).catch(function(){cb()})
axios.get('/api').then(function(){cb()}).catch(function(){cb()})
axios.get('/api').then(function(){cb()}).catch(function(){cb()})
axios.get('/api').then(function(){cb()}).catch(function(){cb()})