what is 'this' keyword and what's its functions?
-
10java or javascript? – Adam Sep 03 '10 at 16:09
-
4The pity upvote is atrocious, whoever cast it. Remember that when you upvote, you're saying "this question is useful and clear". It's neither. – Andy E Sep 03 '10 at 16:15
-
this == homework, there's no way it could not be. @vikash: Please do not post questions (especially homework!) without showing that you have put some effort into them. – Tomalak Sep 03 '10 at 16:15
4 Answers
The this keyword refers to the current object in the context in which the keyword appears.
Here's an article with more: http://www.quirksmode.org/js/this.html
- 25,952
- 31
- 133
- 193
In Java this refers to the object you are currently in. See http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html
In JavaScript that is true as well, but in a function it can mean the "owner" of a function or the global object (ie window). See http://www.quirksmode.org/js/this.html
- 41,954
- 16
- 104
- 142
The this refers as to the object you are currently in. For example, if your object has a method called doSomething(), you could call the method , from another method in your class by doing:
this.doSomething();
Hope this helps.
- 1,961
- 16
- 17
this refers to the current object, so its methods are those defined by the class that the current object is an instance of.
- 37,138
- 14
- 95
- 164