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

Using "this" with class name

I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was: Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); Both of the parameters are…
skaz
  • 21,962
  • 20
  • 69
  • 98
120
votes
9 answers

Javascript "this" pointer within nested function

I have a question concerning how the "this" pointer is treated in a nested function scenario. Say I insert this following sample code into a web page. I get an error when I call the nested function "doSomeEffects()". I checked in Firebug and it…
JoJoeDad
  • 1,711
  • 3
  • 16
  • 15
117
votes
11 answers

Can "this" ever be null in Java?

Saw this line in a class method and my first reaction was to ridicule the developer that wrote it.. But then, I figured I should make sure I was right first. public void dataViewActivated(DataViewEvent e) { if (this != null) // Do some…
Bryce Fischer
  • 5,336
  • 9
  • 30
  • 36
114
votes
4 answers

TypeScript "this" scoping issue when called in jquery callback

I'm not sure of the best approach for handling scoping of "this" in TypeScript. Here's an example of a common pattern in the code I am converting over to TypeScript: class DemonstrateScopingProblems { private status = "blah"; public run() { …
Jonathan Moffatt
  • 13,309
  • 8
  • 51
  • 49
107
votes
1 answer

"this" is undefined inside map function Reactjs

I'm working with Reactjs, writing a menu component. "use strict"; var React = require("react"); var Menus = React.createClass({ item_url: function (item,categories,articles) { console.log('afdasfasfasdfasdf'); var url='XXX'; …
iamhuy
  • 1,081
  • 2
  • 8
  • 7
106
votes
7 answers

What is the 'global' object in NodeJS

I've just seen a weird behaviour of the this keyword in NodeJS environment. I'm listing them with code. I've run this code with NodeJS v6.x, with a single JavaScript file. While testing with one line of code as follows, whether with or without the…
Arnab Das
  • 3,548
  • 8
  • 31
  • 43
105
votes
7 answers

Typescript "this" inside a class method

I know this is probably painfully basic, but i am having a tough time wrapping my head around it. class Main { constructor() { requestAnimationFrame(this.update); //fine } update(): void { …
Clark
  • 2,598
  • 3
  • 27
  • 41
100
votes
4 answers

Hide all but $(this) via :not in jQuery selector

Advanced title, simple question: How can I do the following in jQuery (hiding everything except $(this))? $("table tr").click(function() { $("table tr:not(" + $(this) + ")").hide(); // $(this) is only to illustrate my problem $("table…
Kordonme
  • 2,314
  • 3
  • 20
  • 32
96
votes
7 answers

Using $this inside a static function fails

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context. How can I get this to work? public static function userNameAvailibility() { $result = $this->getsomthin(); }
Jom
  • 963
  • 1
  • 6
  • 4
94
votes
7 answers

Preserving a reference to "this" in JavaScript prototype functions

I'm just getting into using prototypal JavaScript and I'm having trouble figuring out how to preserve a this reference to the main object from inside a prototype function when the scope changes. Let me illustrate what I mean (I'm using jQuery…
Jimmy
  • 35,686
  • 13
  • 80
  • 98
91
votes
4 answers

VueJS: why is "this" undefined?

I'm creating a component with Vue.js. When I reference this in any of the the lifecycle hooks (created, mounted, updated, etc.) it evaluates to undefined: mounted: () => { console.log(this); // logs "undefined" }, The same thing is also…
thanksd
  • 54,176
  • 22
  • 157
  • 150
90
votes
11 answers

Leaking this in constructor warning

I'd like to avoid (most of the) warnings of Netbeans 6.9.1, and I have a problem with the 'Leaking this in constructor' warning. I understand the problem, calling a method in the constructor and passing "this" is dangerous, since "this" may not have…
asalamon74
  • 6,120
  • 9
  • 46
  • 60
88
votes
4 answers

Difference between Python self and Java this

I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this". I know that "self" is not a keyword while "this" is. And that is pretty much…
Bayko
  • 1,344
  • 2
  • 18
  • 25
81
votes
6 answers

Use of "this" keyword in C++

Possible Duplicate: Is excessive use of this in C++ a code smell When should you use the "this" keyword in C++? Is there any reason to use this-> In C++, is the keyword this usually omitted? For example: Person::Person(int age) { _age =…
moteutsch
  • 3,741
  • 3
  • 29
  • 35
80
votes
9 answers

JavaScript setInterval and `this` solution

I need to access this from my setInterval handler prefs: null, startup : function() { // init prefs ... this.retrieve_rate(); this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL); }, retrieve_rate…
Pablo
  • 28,133
  • 34
  • 125
  • 215