this keyword
The This keyword is typically used inside a function or inside an object.
The location of a function or method declaration affects the meaning of the This keyword.
Typically, this points to the object that the current function is manipulating.
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>this关键字</
title
>
</
head
>
<
body
>
<
script
>
window.onload=function (ev) {
// 调用对象
this.person.eat();
}
var person={};//字面量创建对象
// 设置字面量对象属性
person.name=‘huangshiren‘;
person.age=58;
person.appetite=3;
person.eat=function(){
var value=this.name+‘的饭量是‘+this.appetite;
document.write(value+‘<
br
>‘);
document.write(‘正在吃饭‘);
}
</
script
>
</
body
>
</
html
>Test Questions
1, how to determine the point of this keyword?
For:
- Fun () is a global function, while the declared fun receives a simple function in obj, and does not call (Obj.say () is called function), at this time the fun is a function (function () {alert (THIS.STR);} ), then when the fun () Call function, this point is the window
- Who is calling the function, then this is pointing to WHO
2. Do you want to use this keyword?
Answer: No
Javascript&jquery.this keywords