-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjs this关键字.txt
More file actions
261 lines (174 loc) · 3.95 KB
/
js this关键字.txt
File metadata and controls
261 lines (174 loc) · 3.95 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
1.this 是什么?
JavaScript this 关键词指的是它所属的对象。
它拥有不同的值,具体取决于它的使用位置:
在方法中,this 指的是所有者对象。
单独的情况下,this 指的是全局对象。
在函数中,this 指的是全局对象。
在函数中,严格模式下,this 是 undefined。
在事件中,this 指的是接收事件的元素。
像 call() 和 apply() 这样的方法可以将 this 引用到任何对象。
2.方法中的 this
在对象方法中,this 指的是此方法的“拥有者”。
下面的例子中,this 指的是 p 对象。
p 对象是 xm 方法的拥有者。
<!DOCTYPE html>
JavaScript this 关键词
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表 <b>person</b> 对象。</p>
<p>因为 p 对象“拥有”zm 方法。</p>
<p id="demo"></p>
<script>
var p={
xing:"aaaaa",
ming:"bbbbb",
xm:function(){
return this.xing+" "+this.ming;
}
}
document.getElementById("demo").innerHTML=p.xm();
</script>
</body>
</html>//aaaaa bbbbb
3.单独的 this
在单独使用时,拥有者是全局对象,因此 this 指的是全局对象。
在浏览器窗口中,全局对象是 [object Window]:
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 引用 window 对象:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = this;
</script>
</body>
</html>//[obiect window]
4.在严格模式中,如果单独使用,那么 this 指的是全局对象 [object Window]:
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 引用 window 对象:</p>
<p id="demo"></p>
<script>
"use strict"
var x=this
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>//[obiect window]
5.函数中的 this(默认)
在 JavaScript 函数中,函数的拥有者默认绑定 this。
因此,在函数中,this 指的是全局对象 [object Window]。
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表“拥有” myFunction 的对象:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction();
function myFunction() {
return this;
}
</script>
</body>
</html>
6.函数中的 this(严格模式)
JavaScript 严格模式不允许默认绑定。
因此,在函数中使用时,在严格模式下,this 是未定义的(undefined)。
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<b>this</b> 代表“拥有” myFunction 的对象:</p>
<p id="demo"></p>
<script>
"use strict"
var a=this;
document.getElementById("demo").innerHTML = myFunction();
function myFunction() {
return a;
}
</script>
</body>
</html>
7.事件处理程序中的 this
在 HTML 事件处理程序中,this 指的是接收此事件的 HTML 元素:
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<button onclick="this.style.display='none'">单击来删除我!</button>
</body>
</html>
8.对象方法绑定
在此例中,this 是 person 对象(person 对象是该函数的“拥有者”):
实例
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var p={
x:"hhh",
m:"ggg",
xm:function(){
return this;
}
}
document.getElementById("demo").innerHTML=p.xm();
</script>
</body>
</html>//[object Object]
例子:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var p={
x:"hhh",
m:"ggg",
xm:function(){
return this.x+" "+this.m;
}
}
document.getElementById("demo").innerHTML=p.xm();
</script>
</body>
</html>//[object Object]
换句话说,this.x 意味着 this(person)对象的 x 属性。
9.显式函数绑定
call() 和 apply() 方法是预定义的 JavaScript 方法。
它们都可以用于将另一个对象作为参数调用对象方法。
您可以在本教程后面阅读有关 call() 和 apply() 的更多内容。
在下面的例子中,当使用 person2 作为参数调用 person1.fullName 时,this 将引用 person2,即使它是 person1 的方法:
实例
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript <b>this</b> 关键词</h1>
<p>在本例中,<strong>this</strong> 引用 person2,即使它是 person1 的方法:</p>
<p id="demo"></p>
<script>
var person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person2 = {
firstName:"Bill",
lastName: "Gates",
}
document.getElementById("demo").innerHTML = person1.fullName.call(person2);
</script>
</body>
</html>//Bill Gates