一面
很常规的 this 题
var foo = 1
function main() {
console.log(foo)
var foo = 2
console.log(this.foo)
this.foo = 3
}
main()
输出undefined和1
很常规的异步题
console.log('script start')
setTimeout(function() {
console.log('setTimeout')
}, 0)
new Promise(function(resolve) {
console.log('promise1')
resolve()
})
.then(function() {
console.log('promise2')
})
.then(function() {
console.log('promise3')
})
console.log('script end')
输出
script start
promise1
script end
promise2
promise3
setTimeout
垂直居中
要求写出 4 种, 晚上有很多, 就不赘述了
紧接着问了flex: 0 1各代表什么, 正确答案是flex-grow和flex-shrink, 我答反了, 这道题面试官应该是在考察flex熟不熟
判断链表是否有环, 并且自己写一个测试用例试下
我中间踩了几个坑, 面试官提醒过后写出来了
function ListNode(val, next = null) {
this.val = val
this.next = null
}
function isListContainsLoop(root) {
if (root) {
let fast = root.next
let slow = root
while (fast && fast.next && slow) {
if (fast === slow) {
return true
}
fast = fast.next.next
slow = slow.next
}
}
return false
}
const l1 = new ListNode(1)
const l2 = new ListNode(2)
const l3 = new ListNode(3)
const l4 = new ListNode(4)
// const l5 = new ListNode(5)
l1.next = l2
l2.next = l3
l3.next = l4
l4.next = l2
console.log(isListContainsLoop(l1))
Vue 的watch的用法
题干是
data: {
a: {
b: 5
}
}
要 watch 这个 b 我两种用法都答出来了
new Vue({
watch: {
'a.b': function(newVal, oldVal) {}
}
})
this.$watch('a.b', function(newVal, oldVal) {})
http 缓存介绍一下
就把Expires, Cache-Control, ETag, Last-Modified 各干什么的说一下就好了. 我说完之后还追问了Expires和Cache-Control优先级谁高
跨域如何携带Cookie
大家自己找即可, 最好是把ajax, jsonp各种都扯上, 来龙去脉讲清楚 这道题我答得还可以
数组的算法题
题干是
在数组中找出和值为给定值的两个数
输入
arr = [32, 3, 5, 1, 30, 76, 2, 10, 29]
n = 31
输出:
[[1, 30], [2, 29]]
function find(arr, n) {
}
我先写的循环, 就不放代码了, 写完面试官就提醒我有木有复杂度低的方法, 我想不出来了, 面试官很 nice, 提醒了我可以 hash, 然后我就把答案写出来了, 算法还是太菜了呀/泪奔
function find2(arr, n) {
const results = []
const hash = {}
for (let i = 0; i < arr.length; ++i) {
const subtract = n - arr[i]
if (hash[subtract]) {
results.push([arr[i], subtract])
}
if (hash[arr[i]] === undefined) {
hash[arr[i]] = true
}
}
return results
}
const arr = [32, 3, 5, 1, 30, 76, 2, 10, 29, 15]
console.log(find2(arr, 30))
二面
上来先来了一道 Typescript 的题
let foo = {}
for (let i = 0; i < 100; i++) {
foo[String(i)] = i
}
会报错吗, 怎么改, 我一开始答不会报错 2333, 面试官嗯了一下, 我就再看了一遍写出来了
let foo: { [key: srting]: number } = {}
for (let i = 0; i < 100; i++) {
foo[String(i)] = i
}
写完之后还追问了 ts 里String和string的区别
interface 和 type 的区别
这个我有准备到, 所以也没什么难度
具体答案网上有, 我还说到了type就是别名, 类似于其他语言的宏, 不知道有加分不
运行商劫持改怎么解决
问到这个我是没想到的....我其实很想说我是斗不过运营商的, 只能瞎扯了一点DNS啥的. 最后面试官公布答案是要用https, 然后又叫我简述下https和http的区别, 哭了
简述下http
我就扯了一点, 大家最好有所准备[捂脸]
localStorage可以跨域共享吗
我答的不可以, 和域名绑定的. 然后追问要共享怎么共享呢, 我答的postMessage方法, 监听message事件
在canvas里引用别人的图片能不能涂涂画画
好像是问的这个, 我也答得不是很好
进程和线程的区别
我把我压箱底的操作系统知识翻出来一点解释完后, 面试官又追问了怎么理解js是一门单线程语言
Vue3.0新增了什么, 解决了什么问题
大家网上自己找即可, 我这道题答得不是很好
模拟写 jQuery 的链式调用
jQuery 我之前写过, 所以这道题还好
function $(selector) {
const node = document.querySelectorALL(selector)
if (node.length === 1) {
// 判断长度, 是一个怎么样
} else {
// 不止一个怎么样
}
return {
css() {
//..
return node
},
hide() {
// ..
return node
}
}
}
new 的题目
var a = new Foo() // => {id: 1}
var b = new Foo() // => {id: 2}
写出这样一个 Foo 函数 我第一个写法是用的函数的静态变量
function Foo1() {
if (Foo.id === undefined) {
Foo.id = 1
} else {
Foo.id++
}
this.id = Foo.id
}
估计面试官要考我闭包, 先认同了我的写法, 然后问我有木有闭包的写法 中间磕磕绊绊, 在面试官 nice 地提醒下最终还是写出来了
const Foo2 = (function() {
var id = 1
return function() {
this.id = id++
}
})()
然后面试官又问能不能不用new也可以达到一样的效果, 这个我会, 就直接写了
const Foo2 = (function() {
var id = 1
return function() {
if (new.target === undefined) {
const obj = Object.create(Foo2)
obj.id = id++
return obj
} else {
this.id = id++
}
}
})()
var a = Foo2()
console.log(a.id)
var b = Foo2()
console.log(b.id)
然后面试官又追问了instanceof的原理, 我就说a instanceof b,就是看a的constructor是不是b
三面
项目的问题
超级难的题目
function circleNum(image: any): num {}
题干就是给你这样一张图片, 给你这样一个函数, image参数的类型你自己定, 判断图中多少个点
这道题我就不放代码了, 开放性题目, 我最后写的是: image当做string[][], 根据颜色来判断, 当做孤岛问题用递归来做的, 最后也不知道怎么样....