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
59
votes
7 answers

requestAnimationFrame with this keyword

I'm using webkitRequestAnimationFrame but I'm having trouble using it inside of an object. If I pass the this keyword it will use window and I can't find a way for it to use the specified object instead. Example: Display.prototype.draw =…
Ryan
  • 3,726
  • 3
  • 26
  • 38
53
votes
4 answers

Why is JavaScript bind() necessary?

The problem in example 1 is 'this' referring to the global name instead of the myName object. I understand the use of bind() in setting the value of this to a specific object, so it solves the problem in example 1, but why does this problem occur in…
Tom
  • 1,636
  • 2
  • 13
  • 21
52
votes
2 answers

Java leaking this in constructor

Why do IDE's complain about "leaking this in constructor"? I've always assumed that it's just bad practice. But I actually never found why it is bad.
Reinard
  • 3,624
  • 10
  • 43
  • 61
52
votes
3 answers

*this vs this in C++

I understand what this does, but what is the difference between *this and this? Yes, I have Googled and read over *this in my text book, but I just don't get it...
anon235370
50
votes
12 answers

Is the "this" pointer just a compile time thing?

I asked myself whether the this pointer could be overused since I usually use it every single time I refer to a member variable or function. I wondered if it could have performance impact since there must be a pointer which needs to be dereferenced…
Yastanub
  • 1,227
  • 8
  • 19
48
votes
8 answers

What does "this()" method mean?

I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v =…
Sugihara
  • 1,091
  • 2
  • 20
  • 35
48
votes
9 answers

Why can't we use 'this' keyword in a static method

class Sub { static int y; public static void foo() { this.y = 10; } } I understand that this represents the object invoking the method and that static methods are not bound to any object. But in the above mentioned case, the…
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
48
votes
2 answers

How can I use multiple constructors to remove duplicated code while maintaining readability?

int a, b, c; Constructor() { a = 5; b = 10; c = 15; //do stuff } Constructor(int x, int y) { a = x; b = y; c = 15; //do stuff } Constructor(int x, int y, int z) { a = x; b = y; c = z; //do stuff } To…
Cheese
  • 1,429
  • 3
  • 16
  • 17
47
votes
7 answers

JS call static method from class

I have a class with a static method: class User { constructor() { User.staticMethod(); } static staticMethod() {} } Is there an equivalent to this for static methods (i.e. refer to the current class without an…
Chris
  • 13,100
  • 23
  • 79
  • 162
45
votes
1 answer

"This" within es6 class method

For some reason I'm getting weird values for "this" in my es6 class... 'use strict'; class Clicker { constructor(element) { this.count = 0; this.elem = element; this.elem.addEventListener('click', this.click); // logs…
Andrew Luhring
  • 1,762
  • 1
  • 16
  • 37
45
votes
3 answers

How to document the properties of the object in the JSDoc 3 tag: @this

The @param tag allow the documentation of properties, e.g. /** * @param {Object} userInfo Information about the user. * @param {String} userInfo.name The name of the user. * @param {String} userInfo.email The email of the user. */ How…
Matt
  • 4,261
  • 4
  • 39
  • 60
42
votes
7 answers

Should I use `this` within a class?

Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember? What is considered better style? Is there any performance difference? (I am not talking about the case where a local variable has…
Ben
  • 15,938
  • 19
  • 92
  • 138
42
votes
2 answers

In a templated derived class, why do I need to qualify base class member names with "this->" inside a member function?

While I investigate source code of Qt I saw that trolltech guys explicitly use this keyword to access a field on destructor. inline ~QScopedPointer() { T *oldD = this->d; Cleanup::cleanup(oldD); this->d = 0; } So, what's the point of…
useraged
  • 1,706
  • 17
  • 34
42
votes
1 answer

Calling `this` member function from generic lambda - clang vs gcc

Issue: passing a generic lambda (to a template function) that captures this and calls a member function of this without an explicit this-> does not compile on gcc. If the lambda is not generic, or if the lambda is not passed to any other function…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
40
votes
7 answers

Android: why must use getBaseContext() instead of this

this often to reference to current context. But, at some case, why we must use getBaseContext() instead of this. (It means when use this will notice error). Here is my example: Spinner spinner = (Spinner)…
hqt
  • 29,632
  • 51
  • 171
  • 250