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
29
votes
1 answer

The correct way to bind object to Promise.then() argument

I found out the hard way that one can't simply just pass in a object's function into the Bluebird then. I'm assuming that Bluebird's then is doing some magic and wrapping the passed in function in an anonymous function. So I attached a .bind to the…
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
28
votes
2 answers

Call static method from instance in PHP, future deprecation?

While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
28
votes
2 answers

When the dereference operator (*) is overloaded, is the usage of *this affected?

For example, class Person{ string name; public: T& operator*(){ return name; } bool operator==(const Person &rhs){ return this->name == rhs.name; } bool operator!=(const…
user7547935
28
votes
6 answers

Nodejs, express routes as es6 classes

I want to clean up my project a bit and now i try to use es6 classes for my routes. My problem is that this is always undefined. var express = require('express'); var app = express(); class Routes { constructor(){ this.foo = 10 } …
Michael Malura
  • 1,131
  • 2
  • 13
  • 30
28
votes
7 answers

C++ using this pointer in constructors

In C++, during a class constructor, I started a new thread with this pointer as a parameter which will be used in the thread extensively (say, calling member functions). Is that a bad thing to do? Why and what are the consequences? My thread start…
gilbertc
  • 1,049
  • 1
  • 10
  • 19
28
votes
2 answers

Why can we use 'this' as an instance method parameter?

What is a receiver parameter in Java ? Java 8 Language Specification talks about this.
Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
28
votes
7 answers

How does the "this" keyword in Java inheritance work?

In the below code snippet, the result is really confusing. public class TestInheritance { public static void main(String[] args) { new Son(); /* Father father = new Son(); System.out.println(father); //[1]I know…
Garnett
  • 403
  • 1
  • 6
  • 14
28
votes
4 answers

Why declare properties on the prototype for instance variables in JavaScript

I'm trying to get my head around this black art called JavaScript - and, I must admit, pretty excited about it. I've been looking at code examples, mainly from "easeljs" since that is what I will be using mainly. And I'm a bit confused.. I (think I)…
DaveM
  • 418
  • 1
  • 4
  • 7
28
votes
5 answers

this: Cannot use this in static context

Can you please help me with below code. The error is: "Cannot use This in a static context" public class Sample2 { /** * @param args */ public static void main(String[] args) { Sample2 sam=new Sample2(); …
Cyborgz
  • 641
  • 1
  • 9
  • 18
27
votes
8 answers

C# When To Use "This" Keyword

Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the This keyword is used to refer to an instance of the class, however, suppose I have a class called Life, which defines two fields, the person (their name) and…
Goober
  • 13,146
  • 50
  • 126
  • 195
27
votes
3 answers

this vs $(this)

Possible Duplicate: jQuery $(this) vs this I'm new to this and trying to get my concept right. There has been many instances of the use of "this" and "$(this)". Can someone please explain the difference and in what condition that we use the two…
user864600
  • 413
  • 2
  • 5
  • 11
27
votes
2 answers

How to use $this in closure in php

I have function like this: class Service { function delete_user($username) { ... $sessions = $this->config->sessions; $this->config->sessions = array_filter($sessions, function($session) use ($this){ return…
jcubic
  • 61,973
  • 54
  • 229
  • 402
27
votes
2 answers

The 'this' pointer in the initialization list of the constructor

I guess I am unable to understand why this is not working. I always thought that I can use 'this' pointer inside the constructor, but I never knew that I cannot use 'this' in the initialization list. #include class A { public: …
Hemant Bhargava
  • 3,251
  • 4
  • 24
  • 45
27
votes
9 answers

Is it ever justified to have an object which has itself as a field?

Is it ever justified to have an object which has itself as a field like this : class Thing { Thing field; public Thing() { this.field = this; } } I'm not talking about a class with a field of the same type but a class made so…
Autar
  • 1,589
  • 2
  • 25
  • 36
27
votes
12 answers

Is there a name for "this" in Java?

Eclipse will give an error, "The left-hand side of an assignment must be a variable", when I try something like: public class Thing{ String a1; int a2; public void meth(){ Thing A = new Thing(); this = A; } } I had to assign each…
Jay
  • 396
  • 3
  • 6