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

jQuery remove selected option from this

first post here, I come in peace :) I've searched but can't quite find what I'm after. I am trying to manipulate the selected option of a select box. Can someone please explain why this works: $('#some_select_box').click(function() { …
odavy
  • 485
  • 1
  • 4
  • 10
33
votes
3 answers

Difference in context this and getContext()

What is difference between this and getContext(), when I say this I mean this within an Activity.
Lukap
  • 31,523
  • 64
  • 157
  • 244
33
votes
4 answers

CoffeeScript: How to use both fat arrow and this?

I have a coffeescript class that has some jquery event listeners. I would like to use the fat arrow => to avoid having to reference the class, but I still need a reference to the element that would usually be used with this. How can I use…
Andrew
  • 227,796
  • 193
  • 515
  • 708
33
votes
4 answers

What's the difference between this and Activity.this

For example Intent intent = new Intent(this, SecondActivity.class); eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class) Intent intent = new…
user1325996
  • 333
  • 1
  • 3
  • 4
32
votes
3 answers

C++ equivalent to Java this

In Java you can refer to the current object by doing: this.x = x. How do you do this in C++? Assume that each of these code examples are part of a class called Shape. Java: public void setX(int x) { this.x = x; } C++: public: void setX(int…
nobody
  • 323
  • 1
  • 3
  • 4
32
votes
2 answers

console.log() called on object other than console

I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it. For example: const callWithTest = callback =>…
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
32
votes
3 answers

Can I change the context of javascript "this"?

var UI$Contract$ddlForm_change = function() { //'this' is currently the drop down that fires the event // My question is can I change the context so "this" represents another object? this = SomeObject; // then call methods on the…
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
31
votes
3 answers

What is an "incompletely constructed object"?

Goetz's Java Concurrency in Practice, page 41, mentions how this reference can escape during construction. A "don't do this" example: public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( …
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
31
votes
2 answers

What is the type of decltype(this) in C++?

Apparently clang thinks decltype(this) is a pointer to the cv-qualified class, while gcc thinks it is a const reference to a pointer to the cv-qualified class. GCC only thinks decltype(&*this) is a pointer to the cv-qualified class. This has some…
kccqzy
  • 1,538
  • 1
  • 14
  • 22
30
votes
2 answers

Why is "this" undefined in this class method?

I've tried to search over what seems to be the entire internet, but I'm still vexed by a problem with a JS class I'm writing for a Micro Service (still in learning a bit). So, I try to call a class method on an instantiated object, and according to…
KarlGdawg
  • 351
  • 1
  • 4
  • 9
30
votes
5 answers

Can't get $(this) working in jQueryUI autocomplete

I'm trying to create a generic autocomplete script using jQueryUI. The autocomplete should work for every: ... Now I'm trying to access 'foo' or…
bart
  • 14,958
  • 21
  • 75
  • 105
30
votes
5 answers

How is the 'this' variable in Java actually set to the current object?

Consider: class TestParent{ public int i = 100; public void printName(){ System.err.println(this); //{TestChild@428} according to the Debugger. System.err.println(this.i); //this.i is 100. } } class TestChild extends TestParent{ …
Kevin Park
  • 318
  • 5
  • 8
30
votes
5 answers

Are there any consequences from using *this to initialise a class?

In a small game I'm writing, I have a class Weapon with two constructors, one which takes in some parameters to produce a custom weapon, and one that grabs a default one (the CHAIN_GUN): Weapon::Weapon (void) { // Standard weapon *this =…
KKOrange
  • 590
  • 4
  • 14
30
votes
8 answers

Should I use "this" keyword when I want to refer to instance variables within a method?

My teacher says that when I try to access an instance variable within a method I should always use the this keyword, otherwise I would perform a double search. A local scope search and then an instance scope search. Example: public class Test(){ …
Juan Herrera
  • 810
  • 1
  • 10
  • 16
29
votes
8 answers

How to pass $(this) properly in click jQuery function

I am trying to make a tictactoe project in jQuery and I am having a major problem... The tiles are in tags and I am trying to make it so that when the user clicks the the tile, it calls the "marked" function. If we now look into the "marked"…
wayfare
  • 699
  • 2
  • 9
  • 17