面试之手写代码

前言 有些内容虽然不用,但是不代表面试不考。这里为大家送上 5 道常见的手写面试题,希望你们在面试中能遇到某一道也好,这样这篇文章就没有白写。
# 模仿实现 new 创建对象
/**
* 模仿 new
* @return {}
*/
function createNew() {
let obj = {}
let context = [].shift.call(arguments) // 获取构造函数
obj.__proto__ = context.prototype
context.apply(obj, [...arguments])
return obj
}
// @test
function Person(name) {
this.name = name
}
let p = createNew(Person, 'kaola')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 模仿实现 instanceof
/**
* 模仿实现 instanceof
* @param left [左侧参数为一个实例对象]
* @param right [右侧为要判断的构造器函数]
* @return [true / false]
*/
function instanceOf(left, right) {
let prototype = right.prototype // 获取目标原型对象
left = left.__proto__
while (true) {
if (left == null) {
return false
} else if (left == prototype) {
return true
}
left = left.__proto__
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 模仿实现 call
/**
* 模仿call
* @param context [要绑定的this对象]
* @return [返回函数执行结果]
*/
Function.prototype.call_new = function(context) {
let context = context || window
context.fn = this
let args = [...arguments].clice(1)
let result = context.fn(...args)
delete context.fn
return result
}
// @test
foo.call_new(obj, 1, 2, 3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 模仿实现 apply
/**
* 模仿apply
* @param context [要绑定的this对象]
* @return [执行结果]
*/
Function.prototype.apply_new = function(context) {
let context = context || window
context.fn = this
let args = [...arguments][1]
let result
if (args) {
result = context.fn(...args)
} else {
result = context.fn()
}
delete context.fn
return result
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 模仿实现 bind
/**
* 模仿 bind
* @param context [要绑定的this对象]
* @return [执行结果]
*/
Function.ptototype.bind_new(context) {
let self = this;
let args = [...arguments].slice(1);
return function() {
let args1 = [...arguments].slice(1);
return self.apply(context, args.concat(args1));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13