-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path闭包.html
58 lines (52 loc) · 1.23 KB
/
闭包.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
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>闭包!</title>
</head>
<body>
<button id="btn" onclick="AutoIncrease()">
1
</button>
<script>
var AutoIncrease = function() {
console.log(`hello`);
return (function() {
let x = document.getElementById("btn").innerHTML;
console.log(x);
document.getElementById("btn").innerHTML = parseInt(x) + 1;
})();
};
function _Auto() {
let a = 0;
return function() {
a += 1;
return a;
};
}
let a1 = _Auto();
for (let i = 0; i < 10; i++) {
console.log(a1());
}
function create_counter(initial) {
var x = initial || 0;
return {
inc: function() {
x += 1;
return x;
}
};
}
var c1 = create_counter();
for (let i = 0; i < 10; i++) {
console.log(c1.inc()); // 1
}
//WTF!!
var Add = (i = 0) => {
return () => ++i;
};
var v = Add();
</script>
</body>
</html>