-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_with.html
More file actions
53 lines (40 loc) · 941 Bytes
/
07_with.html
File metadata and controls
53 lines (40 loc) · 941 Bytes
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<!--
作用: 用于简化对象操作
-->
<script>
var foo = 1;
var bar = {
foo : 2,
say(){
console.log('hello');
}
};
// with 语句里面不需要再使用 bar. 语句,可直接使用该语句的方法或属性
with(bar){
console.log(foo); // 2
foo = 3;
console.log(foo); // 3
// 一样会覆盖 bar 的 foo属性
var foo = 4;
console.log(foo); // 4
// 当设置不存bar中的属性,会设置到全局变量中,不会设置给 bar(变量污染)
test = 10;
say(); // hello
}
console.log(bar.foo); // 4
console.log(foo); // 1
foo = 5;
console.log(foo); // 5
console.log(foo.test); // undefined
console.log(test); // 10
</script>