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

Javascript: Do I need to put this.var for every variable in an object?

In C++, the language I'm most comfortable with, usually one declares an object like this: class foo { public: int bar; int getBar() { return bar; } } Calling getBar() works fine (ignoring the fact that bar might be uninitialized). The…
Chaosed0
  • 949
  • 1
  • 10
  • 20
39
votes
4 answers

React: this is null in event handler

I have a LoginForm component. I want to check before submit, that both loginName and password is set. I tried with this code (a lot of stuff omitted): class LoginForm extends Component { constructor() { super(); this.state = { …
Midiparse
  • 4,701
  • 7
  • 28
  • 48
39
votes
1 answer

Call parent function which is being overridden by child during constructor chain in JavaScript(ES6)

I've encounter a problem below with JavaScript(ES6) class A{ constructor(){ this.foo(); } foo(){ console.log("foo in A is called"); } } class B extends A{ constructor(){ super(); this.foo(); } foo(){ …
andrew
  • 430
  • 1
  • 6
  • 8
39
votes
5 answers

Arrow Functions and This

I'm trying out ES6 and want to include a property inside my function like so var person = { name: "jason", shout: () => console.log("my name is ", this.name) } person.shout() // Should print out my name is jason However, when I run this code…
thank_you
  • 11,001
  • 19
  • 101
  • 185
39
votes
5 answers

C# way to add value in a List at index

Is there any way you can add a value at a specific index? I try to do indexator and I have Lists. Is there any trick for making this this in this context :D public class Multime { private List> multiSets; private List
TyGerX
  • 513
  • 1
  • 6
  • 11
38
votes
2 answers

Let Resharper force this keyword on fields

Does anyone know if it is possible to have resharper force the use of the this keyword when it can be used? For fields and such... Resharper is capable of showing where it can be removed, but our coding standard tells us to use the this keyword.
Sorskoot
  • 10,190
  • 6
  • 55
  • 98
37
votes
4 answers

this value in JavaScript anonymous function

Can anybody explain to me why A is true and B is false? I would have expected B to be true as well. function MyObject() { }; MyObject.prototype.test = function () { console.log("A", this instanceof MyObject); (function () { …
Corno
  • 5,448
  • 4
  • 25
  • 41
37
votes
1 answer

Compilation error : 'this' cannot be implicitly captured in this context

I am trying to add a condition_variable to handle threads, but get a compilation error at this line: this->cv.wait(lk, []{return this->ready;}); Looks like the for the variable this->ready, the this is not in the right scope. In Java this can be…
Ray
  • 16,025
  • 5
  • 31
  • 51
37
votes
5 answers

Access to static properties via this.constructor in typescript

I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor, but in typescript…
36
votes
2 answers

Does the comma operator influence the execution context in Javascript?

var a = 1; var b = { a : 2, c : function () { console.log(this.a); } }; b.c(); // logs 2 (b.c)(); // logs 2 (0, b.c)(); // logs 1 The first is understandable, for "this" is pointed to Object "b". But why does the second one log the same…
Ziqi
  • 423
  • 3
  • 8
36
votes
1 answer

Is std::move(*this) a good pattern?

In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun()…
alfC
  • 14,261
  • 4
  • 67
  • 118
35
votes
5 answers

What can a 'const' method change?

C++ methods allow a const qualifier to indicate that the object is not changed by the method. But what does that mean? Eg. if the instance variables are pointers, does it mean that the pointers are not changed, or also that the memory to which they…
Daniel
  • 924
  • 1
  • 11
  • 16
35
votes
4 answers

Passing "this" to a function from within a constructor?

Can I pass "this" to a function as a pointer, from within the class constructor, and use it to point at the object's members before the constructor returns? Is it safe to do this, so long as the accessed members are properly initialized before the…
nescire
35
votes
5 answers

The invocation context (this) of the forEach function call

I was wondering what the 'this' value (or invocation context) is of the forEach callback function. This code doesn't seem to work: var jow = [5, 10, 45, 67]; jow.forEach(function(v, i, a){ this[i] = v + 1; }); alert(jow); Thx for explaining…
kevinius
  • 4,232
  • 7
  • 48
  • 79
34
votes
6 answers

Can 'this' ever be null in Javascript

I have a function along the lines of the following: doSomething: function () { var parent = null; if (this === null) { parent = 'some default value'; } else { parent = this.SomeValue(); } …
Mark Robinson
  • 13,128
  • 13
  • 63
  • 81