-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall apply bind实现.html
executable file
·108 lines (88 loc) · 3.69 KB
/
call apply bind实现.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<title>title</title>
</head>
<body>
<script>
var foo = {
value: 1
};
function test(name, age) {
console.log(name, age, this.value)
}
Function.prototype.call1 = function(content) {
var content = content || window
content.fn = this
var args = []
for(var i = 1, len = arguments.length; i < len; i++) {
args.push('arguments['+i+']')
}
eval('content.fn(' + args +')')
delete content.fn
}
// test.call1(foo, 'kevin', 18);
Function.prototype.bind1 = function(content) {
var self = this
var args = Array.prototype.slice.call(arguments, 1)
var fBound = function(name) {
var bindArgs = Array.prototype.slice.call(arguments);
return self.apply(content, args.concat(bindArgs))
}
return fBound
}
// var testBind = test.bind1(foo, 'xq', 18)
// testBind()
// this指向是foo 并不是a
// var a = new testBind('xq')
// 问题:一个绑定函数也能使用new操作符创建对象:这种行为就像把原函数当成构造器。提供的 this 值被忽略,同时调用时的参数被提供给模拟函数。
Function.prototype.bind2 = function(content) {
var self = this
var args = Array.prototype.slice.call(arguments, 1)
var fBound = function() {
var bindArgs = Array.prototype.slice.call(arguments);
return self.apply(this instanceof fBound ? this : content, args.concat(bindArgs))
}
fBound.prototype = this.prototype
return fBound
}
test.prototype.a = 1
// 原生bind
// var testBind = test.bind(foo, 'xq', 18)
// // testBind() // xq, 18, 1
// var nObj = new testBind('xq') // xq, 18, undefined this指向nObj new关键字
// var testBind = test.bind2(foo, 'xq', 18)
// testBind() // xq, 18, 1
// var nObj = new testBind() // xq, 18, undefined this指向nObj new关键字
// console.log(nObj.a)
// 当修改新函数的prototype会影响到原函数的prototype
// testBind.prototype.b = 2
// var tt = new test('xq', 18)
// 原函数未添加 但是可以访问到
// console.log(tt.b)
Function.prototype.bind3 = function(content) {
var self = this
var args = Array.prototype.slice.call(arguments, 1)
// 中转
var fNOP = function() {}
var fBound = function() {
var bindArgs = Array.prototype.slice.call(arguments);
return self.apply(this instanceof fBound ? this : content, args.concat(bindArgs))
}
fNOP.prototype = this.prototype
fBound.prototype = new fNOP()
// 构造函数实例化 fBound实例的指向fNOP.prototype --chain-- 具有this.prototype中的属性 在修改fBound不会影响到原函数的原型对象
return fBound
}
// var testBind = test.bind3(foo, 'xq', 18)
// testBind() // xq, 18, 1
// var nObj = new testBind() // xq, 18, undefined this指向nObj new关键字
// console.log(nObj.a)
// var tt = new test('xq', 18)
// 原函数未添加 访问不到
// console.log(tt.b)
</script>
</body>
</html>