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
1881
votes
15 answers

How to access the correct `this` inside a callback

I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); }); } // Mock transport object var transport = { …
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1483
votes
22 answers

How does the "this" keyword work, and when should it be used?

I am looking to find a clear explanation of what the "this" keyword does, and how to use it correctly. It seems to behave strangely, and I don't fully understand why. How does this work and when should it be used?
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
1054
votes
7 answers

'this' vs $scope in AngularJS controllers

In the "Create Components" section of AngularJS's homepage, there is this example: controller: function($scope, $element) { var panes = $scope.panes = []; $scope.select = function(pane) { angular.forEach(panes, function(pane) { …
Alexei Boronine
  • 11,061
  • 4
  • 19
  • 14
990
votes
24 answers

Javascript call() & apply() vs bind()?

I already know that apply and call are similar functions which set this (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? var obj = { x: 81, getX:…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
830
votes
16 answers

Use of 'prototype' vs. 'this' in JavaScript?

What's the difference between var A = function () { this.x = function () { //do something }; }; and var A = function () { }; A.prototype.x = function () { //do something };
sw234
  • 8,309
  • 3
  • 17
  • 3
648
votes
10 answers

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

What is the difference between getContext() , getApplicationContext() , getBaseContext() , and "this"? Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible.
iCurious
  • 8,161
  • 7
  • 28
  • 46
575
votes
7 answers

What's the difference between '$(this)' and 'this'?

I am currently working through this tutorial: Getting Started with jQuery For the two examples below: $("#orderedlist").find("li").each(function (i) { $(this).append(" BAM! " + i); }); $("#reset").click(function () { $("form").each(function…
Kevin Wu
  • 8,461
  • 5
  • 28
  • 28
390
votes
6 answers

What does 'var that = this;' mean in JavaScript?

In a JavaScript file I saw: function Somefunction(){ var that = this; ... } What is the purpose of declaring that and assigning this this to it?
Chris
  • 11,780
  • 13
  • 48
  • 70
358
votes
8 answers

Use of "this" keyword in formal parameters for static methods in C#

I've come across several instances of C# code like the following: public static int Foo(this MyClass arg) I haven't been able to find an explanation of what the this keyword means in this case. Any insights?
kpozin
  • 25,691
  • 19
  • 57
  • 76
358
votes
17 answers

When should I use "this" in a class?

I know that this refers to a current object. But I do not know when I really need to use it. For example, will be there any difference if I use x instead of this.x in some of the methods? May be x will refer to a variable which is local for the…
Roman
  • 124,451
  • 167
  • 349
  • 456
315
votes
6 answers

Pass correct "this" context to setTimeout callback?

How do I pass context into setTimeout? I want to call this.tip.destroy() if this.options.destroyOnHide after 1000 ms. How can I do that? if (this.options.destroyOnHide) { setTimeout(function() { this.tip.destroy() }, 1000); } When I try the…
JamesBrownIsDead
  • 4,655
  • 6
  • 30
  • 29
279
votes
7 answers

When to call activity context OR application context?

There has been a lot of posting about what these two contexts are.. But I'm still not getting it quite right As I understand it so far: Each is an instance of its class which means that some programmers recommend you to use…
Norfeldt
  • 8,272
  • 23
  • 96
  • 152
255
votes
7 answers

How does "this" keyword work within a function?

I just came across an interesting situation in JavaScript. I have a class with a method that defines several objects using object-literal notation. Inside those objects, the this pointer is being used. From the behavior of the program, I have…
rmeador
  • 25,504
  • 18
  • 62
  • 103
254
votes
5 answers

How do I pass the this context to a function?

I thought this would be something I could easily google, but maybe I'm not asking the right question... How do I set whatever "this" refers to in a given javascript function? for example, like with most of jQuery's functions such as: …
user126715
  • 3,648
  • 3
  • 23
  • 25
248
votes
31 answers

When do you use the "this" keyword?

I was curious about how other people use the this keyword. I tend to use it in constructors, but I may also use it throughout the class in other methods. Some examples: In a constructor: public Light(Vector v) { this.dir = new…
ftdysa
  • 1,242
  • 6
  • 17
  • 26
1
2 3
99 100