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
27
votes
6 answers

How can you assign a value to the pointer 'this' in C++

In a function, how to you assign this a new value?
Astra Meyers
  • 303
  • 1
  • 3
  • 4
26
votes
7 answers

jQuery/JavaScript "this" pointer confusion

The behavior of "this" when function bar is called is baffling me. See the code below. Is there any way to arrange for "this" to be a plain old js object instance when bar is called from a click handler, instead of being the html element? // a class…
I. J. Kennedy
  • 24,725
  • 16
  • 62
  • 87
26
votes
2 answers

How can I get rid of the `this` keyword in local functions?

I'm writing a small JavaScript game framework and often use objects' properties, like this.depth = this.y; But these this'es are quite annoying @_@. Is there a way to write just… depth = y; …not affecting global object? My instances are created…
26
votes
4 answers

jQuery if "this" contains

I'm trying to change any elements containing a particular text string to a red color. In my example I can get the child elements to become blue, but there's something about the way I've written the 'Replace Me' line that is incorrect; the red color…
Chris
  • 463
  • 2
  • 6
  • 16
26
votes
3 answers

Javascript scope addEventListener and this

I am a C# developer experimenting with JavaScript and I'm trying to get my head around the scope :) I have the following code which contains an addEventListener in which I want to use a field from my object: (function(window) { function…
Raf
  • 663
  • 1
  • 8
  • 21
24
votes
7 answers

Why is assignment to 'this' not allowed in java?

The error I get from the compiler is "The left hand side of an assignment must be a variable". My use case is deep copying, but is not really relevant. In C++, one can assign to *this. The question is not how to circumvent assignment to this. It's…
kostja
  • 60,521
  • 48
  • 179
  • 224
24
votes
1 answer

Why `this.synchronized` instead of just `synchronized` in Scala?

In an example of working with JDBC in Scala, there is a following code: this.synchronized { if (!driverLoaded) loadDriver() } Why this.synchronized instead of just synchronized?
Ivan
  • 63,011
  • 101
  • 250
  • 382
24
votes
4 answers

Does every c++ member function take `this` as an input implicitly?

When we create a member function for a class in c++, it has an implicit extra argument that is a pointer to the calling object -- referred as this. Is this true for any function, even if it does not use this pointer. For example, given the…
rtpax
  • 1,687
  • 1
  • 18
  • 32
24
votes
9 answers

Difference between this and base

I am interested to know the difference between this and base object in C#. What is the best practice when using them?
Abhishek
  • 957
  • 3
  • 20
  • 43
24
votes
4 answers

Possible segmentation fault: Am I using the "this->" operator correctly?

I am doing a homework problem that I have a question about. If you don't feel comfortable assisting with a homework problem, I should say that my instructor has encouraged us to ask for help on this site when we are completely stumped. Also, I have…
shermanzach
  • 591
  • 1
  • 6
  • 14
24
votes
2 answers

Is it bad to have the same name for parameter as for member variable?

For example, is this any of the following Bad practice Unreadable Inefficient (the call to this pointer) Any other reason why it's bad to do this . class Person { public: string name; Person(string name) { …
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
24
votes
5 answers

onClick Function "this" Returns Window Object

I've come across a head scratching issue with my JavaScript application. If I write an element like this:
  • I get "LI." However if I do this:
  • Where "foo()" is: function foo(){…
    Pori
    • 696
    • 3
    • 7
    • 18
    24
    votes
    15 answers

    Excessive use of `this` in C++

    I'm dealing with a large code base that uses the following construct throughout class MyClass { public: void f(int x); private: int x; }; void MyClass::f(int x) { ' ' this->x = x; ' ' } Personally, I'd always used and hence prefer the…
    SmacL
    • 22,555
    • 12
    • 95
    • 149
    23
    votes
    3 answers

    Is it safe to return *this as a reference?

    Returning reference to this object is often used in assignment operator overloading. It is also used as a base for named parameters idiom which allows to initialize object by chain of calls to setter methods: Params().SetX(1).SetY(1) each of which…
    anton_rh
    • 8,226
    • 7
    • 45
    • 73
    22
    votes
    2 answers

    PHP copy all object properties to this

    I have an object in PHP, of the type MyObject. $myObject instanceof MyObject Now, in the class MyObject, there is a non-static function, and in there, I use the reference to "me", like $this, but I also have another object there. Is it possible,…
    arik
    • 28,170
    • 36
    • 100
    • 156