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
80
votes
12 answers

Java - when to use 'this' keyword

What is the best practise for using the this keyword in Java? For example, I have the following class: class Foo { Bar bar; public Foo(Bar bar) { this.bar = bar; } } That's fine and all, but Java is clever enough to know what…
jackbot
  • 2,931
  • 3
  • 27
  • 35
78
votes
7 answers

How does require() in node.js work?

I tried this: // mod.js var a = 1; this.b = 2; exports.c = 3; // test.js var mod = require('./mod.js'); console.log(mod.a); // undefined console.log(mod.b); // 2 console.log(mod.c); // 3, so this === exports? So I image that require() may…
Trantor Liu
  • 8,770
  • 8
  • 44
  • 64
74
votes
8 answers

Controlling the value of 'this' in a jQuery event

I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible. During the initialisation of my control I wire up various click events like so jQuery('#available input', …
Pat Long - Munkii Yebee
  • 3,592
  • 2
  • 34
  • 68
74
votes
10 answers

What does "this" refer to in arrow functions in ES6?

I've read in several places that the key difference is that this is lexically bound in arrow functions. That's all well and good, but I don't actually know what that means. I know it means it's unique within the confines of the braces defining the…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
71
votes
7 answers

Difference between $(this) and this in jquery

What is the fundamental difference between using $(this) vs this $('.viewComments').click(function(ev){ //returns the desired value alert(this.getAttribute('id')); //Gives an error sayin function is not defined …
Sheldon Fernandes
  • 1,299
  • 1
  • 11
  • 18
70
votes
6 answers

this inside function

My question is: function Foo() { this.foo = "bar"; // <- What is "this" here? } From what I can tell it depends on how Foo is used, i.e. as a constructor or as a function. What can this be in different circumstances?
Frank T. Icali
  • 733
  • 1
  • 5
  • 7
69
votes
5 answers

"this" inside object

I'm trying to calculate a proportional height (while excluding a static height element) from a width that gets passed in via a request (defaults to 560). However, wF.h evaluates to NaN. If I replace this.w with 560 it works, but not when trying to…
mhe
  • 693
  • 1
  • 6
  • 7
69
votes
2 answers

How to function call using 'this' inside forEach loop

In the following object, I have a problem using the 'this' reference: function SampleObject(){ this.addObject = function(object){...} ... // more code here ... this.addNewObjects= function(arr){ arr.forEach( function…
user3339411
  • 887
  • 1
  • 6
  • 10
68
votes
5 answers

The reason to use JS .call() method?

I'm interested what's the reason to have call() method in JS. It seems it duplicates usual method of calling this. For example, I have a code with call(). var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " "…
Green
  • 28,742
  • 61
  • 158
  • 247
64
votes
3 answers

Is it safe to use the "this" pointer in an initialization list?

I have two classes with a parent-child relationship (the Parent class "has-a" Child class), and the Child class has a pointer back to the Parent. It would be nice to initialize the parent pointer upon construction of the child, as follows: class…
bavaza
  • 10,319
  • 10
  • 64
  • 103
64
votes
3 answers

Changing the 'this' variable of value types

Apparently you can change the this value from anywhere in your struct (but not in classes): struct Point { public Point(int x, int y) { this = new Point(); X = x; Y = y; } int X; int Y; } I've neither seen this…
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
62
votes
5 answers

React - TypeError: Cannot read property 'props' of undefined

I'm trying to create a click event be able to delete an item on my list, but when I click it I get "TypeError: Cannot read property 'props' of undefined". I'm trying to stick to ES6 as much as possible, and I'm pretty sure its something to do…
pyan
  • 865
  • 2
  • 12
  • 23
61
votes
5 answers

Why must I use the "this" keyword for forward references?

When I use the this keyword for accessing a non-static variable in a class, Java doesn't give any error. But when I don't use it, Java gives an error. Why must I use this? I know when should normally I use this, but this example is very different…
Nomad
  • 918
  • 6
  • 23
61
votes
6 answers

jQuery $(this) keyword

Why is it important to use $(this) instead of re-selecting the class? I am using a lot of animate and css editing in my code, and I know I can simplify it by using $(this).
agassi0430
  • 1,155
  • 1
  • 13
  • 31
59
votes
4 answers

jQuery Passing $(this) to a Function

I have lines of code like this: $(this).parent().parent().children().each(function(){ // do something }); It works well. But I need to run these lines multiple times. So I have created a function and pass $(this) parameter to a…
Vahid
  • 3,384
  • 2
  • 35
  • 69