-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.html
66 lines (55 loc) · 1.11 KB
/
closure.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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>闭包例子</title>
</head>
<body>
<script>
var count = function () {
var a = 0;
return a++;
};
console.log(count());
console.log(count());
console.log(count());
console.log(count());
count = function () {
var a = 0;
return function () {
return ++a;
};
}();
// 成功实现计数
console.log(count());
console.log(count());
console.log(count());
console.log(count());
console.log(count());
// 一个失败的例子
count = function () {
var a = 0;
return function () {
return ++a;
}();
};
console.log(count());
console.log(count());
console.log(count());
console.log(count());
/**
* 实现参数提前绑定
*/
var curry = function (func, params) {
return function () {
func.apply(null, params);
};
};
var cf = function (name) {
console.log(name);
};
var mxy = curry(cf, ['mxy']);
mxy();
</script>
</body>
</html>