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
22
votes
3 answers

What is the context of "this" in this example?

Why do these two lines create different values for this?

a paragraph

The first one alerts with the file's URI, the second one alerts…
Sheida
  • 243
  • 1
  • 5
22
votes
4 answers

The 'this' keyword as a property

I know C# well, but it is something strange for me. In some old program, I have seen this code: public MyType this[string name] { ......some code that finally return instance of MyType } How is it called? What is the use of this?
viky
  • 17,275
  • 13
  • 71
  • 90
22
votes
7 answers

Using new (this) to reuse constructors

This came up recently in a class for which I am a teaching assistant. We were teaching the students how to do copy constructors in c++, and the students who were originally taught java asked if you can call one constructor from another. I know the…
Brandon Bodnar
  • 8,202
  • 2
  • 36
  • 42
22
votes
2 answers

How to pass context to forEach() anonymous function

What's the modern and correct way to pass the this context to an anonymous forEach function? function Chart() { this.draw = function(data) { data.forEach(function(value) { //do something with values console.log(this);…
mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
22
votes
4 answers

What are all the differences between function and constructor function in JavaScript?

In this blog author says below function is a constructor function: function Cat(name, color) { this.name = name; this.color = color; } Cat.prototype.age = 0; var catC = new Cat("Fluffy", "White"); The instances of Cat function has a name and…
vimal1083
  • 8,499
  • 6
  • 34
  • 50
22
votes
7 answers

Where's the difference between self and $this-> in a PHP class or PHP method?

Where's the difference between self and $this-> in a PHP class or PHP method? Example: I've seen this code recently. public static function getInstance() { if (!self::$instance) { self::$instance = new…
openfrog
  • 40,201
  • 65
  • 225
  • 373
22
votes
1 answer

Why is it called "open (or closed) recursion?

I found some explanations of open/closed recursion, but I do not understand why the definition contains the word "recursion", or how it compares with dynamic/static dispatching. Among the explanations I found, there are: Open recursion. Another…
21
votes
3 answers

return "this" in C++?

In Java you can simply return this to get the current object. How do you do this in C++? Java: class MyClass { MyClass example() { return this; } }
nobody
  • 229
  • 1
  • 2
  • 6
21
votes
5 answers

call() a function within its own context

var f = function() { this.x = 5; (function() { this.x = 3; })(); console.log(this.x); }; f.call(f); f(); f.call(); Running var f as f.call(f) outputs 5. When running it as f() or f.call() outputs 3. What happens in…
El Anonimo
  • 1,759
  • 3
  • 24
  • 38
21
votes
1 answer

$(this) is selecting window object instead of clicked element jquery

I have a really strange issue. I simply want to select a clicked element. I did this a lot of times and it always worked but this time, the jQuery $(this) selector doesn't select the clicked element, it selects the window object. Please let me know,…
Rurpot
  • 239
  • 2
  • 6
21
votes
5 answers

Accessing this from within an object's inline function

I'm having difficulty referencing "this" from within a javascript inline function, within an object method. var testObject = { oThis : this, testVariable : "somestring", init : function(){ console.log(this.testVariable); //…
Matt
  • 1,140
  • 2
  • 10
  • 17
21
votes
1 answer

Jquery UI Sortable - Get the item being sorted

When using Jquery UI Sortable (which is great by the way) how do you get the item that is currently being sorted. When you use $(this); it return the actual sortable list, not the current sorted item. I want to do fancy-pants things with the widget…
CafeHey
  • 5,699
  • 19
  • 82
  • 145
21
votes
5 answers

Why doesn't the compiler at least warn on this == null

Why does the C# compiler not even complain with a warning on this code? : if (this == null) { // ... } Obviously the condition will never be satisfied..
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
21
votes
3 answers

Ignore certain TypeScript compile errors?

I am wondering if there is a way to ignore certain TypeScript errors upon compilation? I basically have the same issues most people with large projects have around using the this keyword, and I don't want to put all my classes methods into the…
Grofit
  • 17,693
  • 24
  • 96
  • 176
21
votes
1 answer

In C++11, how do I specify that the implicit "this" parameter "[[carries_dependency]]"?

In [dcl.attr.depend]/1, I read: The attribute[...] carries_dependency [...] may be applied to the declarator-id of a parameter-declaration in a function declaration or lambda, in which case it specifies that the initialization of the parameter…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90