Questions tagged [this]

In many object-oriented languages, "this" is a reference to the current instance of the class or object. Use with a language tag, like [javascript], [java], [c#], etc.

this is a keyword that refers to the current class instance or object in many object-oriented programming languages.

For example in Java:

class ExampleClass{

    private String name = 'Example';

    public ExampleClass(String name){
        // refer to the name property of this instance
        this.name = name;
        this.initialize();
    }

    public void initialize(){
        // do something here
    }

}

Common questions about the keyword involve the somewhat confusing rules Javascript uses. There are instances in Javascript where the this context can be lost.

Some programming languages use variants of the this keyword, such as Me in Visual Basic and self in Python and Ruby.

See also:

http://en.wikipedia.org/wiki/This_%28computer_programming%29

6292 questions
226
votes
4 answers

How can I exclude $(this) from a jQuery selector?

I have something like this:
A
B
C
When one of these links is clicked, I want to perform the .hide()…
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
220
votes
12 answers

React: "this" is undefined inside a component function

class PlayerControls extends React.Component { constructor(props) { super(props) this.state = { loopActive: false, shuffleActive: false, } } render() { var shuffleClassName = this.state.toggleActive ?…
Maximus S
  • 10,759
  • 19
  • 75
  • 154
204
votes
3 answers

Why is 'this' a pointer and not a reference?

I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type…
Naveen
  • 74,600
  • 47
  • 176
  • 233
188
votes
7 answers

Difference between $(this) and event.target?

I was making tabbed panels, following the tutorial in JavaScript and jQuery : The Missing Manual, there's that first line when the author does this: var target = $(this); But I tried to do it that way var target = evt.target; and I got that…
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118
166
votes
5 answers

What is context in _.each(list, iterator, [context])?

I am new to underscore.js. What is the purpose of [context] in _.each()? How should it be used?
ram
  • 11,468
  • 16
  • 63
  • 89
159
votes
22 answers

What is the meaning of "this" in Java?

Normally, I use this in constructors only. I understand that it is used to identify the parameter variable (by using this.something), if it have a same name with a global variable. However, I don't know that what the real meaning of this is in…
guilgamos
  • 1,695
  • 4
  • 13
  • 5
148
votes
2 answers

Access "this" from Java anonymous class

Given the following code: public interface Selectable { public void select(); } public class Container implements Selectable { public void select() { ... } public void createAnonymousClass() { Selectable s = new Selectable() { …
Bob
  • 5,510
  • 9
  • 48
  • 80
142
votes
5 answers

Set "this" variable easily?

I have a pretty good understanding of Javascript, except that I can't figure out a nice way to set the "this" variable. Consider: var myFunction = function(){ alert(this.foo_variable); } var someObj = document.body; //using body as example…
sam
142
votes
2 answers

std::shared_ptr of this

I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution: Imagine you have an object of class A being parent of an object of…
Icarus
  • 1,746
  • 2
  • 13
  • 12
135
votes
4 answers

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

I'm new to android and I'm trying to understand the difference between getApplication(), getApplicationContext(), getBaseContext(), getContext() and someClass.this and especially when to use the these methods in the following code lines: When I…
Pheonix7
  • 2,131
  • 5
  • 21
  • 38
131
votes
4 answers

What is the difference between Class.this and this in Java

There are two ways to reference the instance of a class within that class. For example: class Person { String name; public void setName(String name) { this.name = name; } public void setName2(String name) { Person.this.name =…
user_1357
  • 7,766
  • 13
  • 63
  • 106
129
votes
4 answers

Why is 'this' undefined inside class method when using promises?

I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3. Is there a more correct way to write this code? function MyClass(opts){ this.options = opts; return this.method1() …
SteamDev
  • 4,294
  • 5
  • 20
  • 29
128
votes
5 answers

Java: Class.this

I have a Java program that looks like this. public class LocalScreen { public void onMake() { aFuncCall(LocalScreen.this, oneString, twoString); } } What does LocalScreen.this means in aFuncCall?
Johnny Jazz
  • 1,759
  • 3
  • 13
  • 5
125
votes
12 answers

When should I make explicit use of the `this` pointer?

When should I explicitly write this->member in a method of a class?
user53670
125
votes
11 answers

What does the variable $this mean in PHP?

I see the variable $this in PHP all the time and I have no idea what it's used for. I've never personally used it. Can someone tell me how the variable $this works in PHP?
waiwai933
  • 14,133
  • 21
  • 62
  • 86