-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy paththis.js
55 lines (52 loc) · 1.26 KB
/
this.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//实现一个bind函数
Function.prototype.myBind = function (context) {
if (typeof context !== 'function') {
throw new TypeError('Error')
}
var _this = this
var argus = [...arguments].slice(1)
//返回一个函数
return function F() {
//因为返回了一个函数,我们可以new F() 所以需要判断
if (this instanceof F) {
return new _this(...arfus, ...arguments, )
}
return _this.apply(context, args.concat(...arguments))
}
}
Function.prototype.call2 = function (context) {
debugger
// 首先要获取调用call的函数,用this可以获取
context.fn = this;
context.fn();
delete context.fn;
}
//实现一个call函数
Function.prototype.myCall = function (context) {
debugger
var context = context || window
context.fn = this
var args = Array.from(arguments).slice(1)
var result = context.fn(...args)
delete context.fn
return result
}
//实现一个apply函数
Function.prototype.myApply = function (context) {
debugger
var context = context || window
context.fn = this
var result
if (arguments[1]) {
result = context.slice
}
return result
}
let handle = {
get(target, name) {
return name in target ? target[name] : 37
}
}
let p = new Proxy({}, handler)
p.a = 9
p.b = undefined