-
Notifications
You must be signed in to change notification settings - Fork 10
/
1.闭包.html
34 lines (33 loc) · 923 Bytes
/
1.闭包.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
// var name="The Window";
// var object={
// name:"My Object",
// getNameFunc:function(){
// return function (){
// return this.name;
// };
// }
// };
//
// console.log(object.getNameFunc()()); //输出的结果是"The Window",这里this指的是全局变量
var name ="The Window";
var object={
name:'My Object',
getNameFunc:function(){
var that=this;
return function (){
return that.name;
};
}
};
console.log(object.getNameFunc()()); //输出的结果是"My Object",这里的that就指明了getNameFunc里面的变量范围
</script>
</head>
<body>
</body>
</html>