Skip to content

Commit c49fcd9

Browse files
author
hans
committed
update note
1 parent 5e8453e commit c49fcd9

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

知识点/js/bind.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<meta charset="UTF-8">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
<script>
12+
// 原生js实现bind方法
13+
Function.prototype.bindX = function() {
14+
// 保存原函数
15+
let self = this
16+
// 保存this、剩余参数
17+
// 方式一
18+
// let [thisArg, ...args] = arguments
19+
// 方式二
20+
let thisArg = arguments[0]
21+
let args = Array.prototype.slice.call(arguments, 1)
22+
23+
return function() {
24+
// 方式一
25+
// self.apply(thisArg, [...args, ...arguments])
26+
// 方式二
27+
self.apply(thisArg, args.concat(Array.prototype.slice.call(arguments)))
28+
}
29+
}
30+
31+
function a(m, n, o) {
32+
console.log(this.name + ' ' + m + ' ' + n + ' ' + o);
33+
}
34+
var b = {
35+
name: 'xing'
36+
}
37+
a.bind(b, 7, 8)(9)
38+
a.bindX(b, 7, 8)(9) // xing 7 8 9
39+
40+
// 扩展
41+
// 箭头函数不绑定Arguments 对象
42+
// 箭头函数只能用于非方法函数
43+
// 箭头函数不能作为构造函数
44+
// 箭头函数没有property属性
45+
</script>
46+
</html>

0 commit comments

Comments
 (0)