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

Why exactly does passing a closure from one class to another and calling it from the second class execute code from the first class?

It's pretty difficult to come up with a title for this question but basically here is the code: closure = $closure; } public function…
user3117244
  • 143
  • 1
  • 8
2
votes
1 answer

What does this.$ mean in Angular?

What does this.$ mean in Angular? I see cases where 'this' is set to a variable such as var vm = this; Then vm is used like vm.$.Something .. What does the vm.$ mean? This is a classy angular example: var app = angular.module('app',…
2
votes
2 answers

java "this" in c++

I'm in a function in java and I create a new Object passing "this" as parameter: class AClass { AClass(TestClass testClass) { } } class TestClass { AClass doSomething() { return new AClass(this); } } How to do That in…
okami
  • 2,093
  • 7
  • 28
  • 40
2
votes
1 answer

Why does the node global context === 'this' only sometimes, in a sample?

I have the following test.js file that emits two lines of output, each line tests for strict equality between the global object and this. var c = require("console"); console.log(this === global); (function () { console.log(this ===…
John K
  • 28,441
  • 31
  • 139
  • 229
2
votes
5 answers

Having the correct value of 'this' in JS

I have two Javascript "objects" similar to so.... var Object2 = new (function() { this.FetchData = function(callback) { // do some stuff callback(data); }; }); var Object1 = new (function() { this.DisplayStuff =…
T. Stone
  • 19,209
  • 15
  • 69
  • 97
2
votes
1 answer

How can i refer the object on jQuery?

I'm making a script which has one ajax call inside an each function. The problem is that on the success callback from ajax call, I want to use the object that we are using at each function. EDIT: Here is some of my code: configs={ general:{ …
cusspvz
  • 5,143
  • 7
  • 30
  • 45
2
votes
1 answer

How can I call a class variable from a callback function?

I have a class that has this structure: public class MyClass { private double myDouble; private MyObject myObject; public myMethod() { AnotherObject anotherObject = new AnotherObject(); …
user1301428
  • 1,743
  • 3
  • 25
  • 57
2
votes
1 answer

Is it possible to point to anonymous class by .THIS keyword?

SSCCE: public class Test { public Test() { new Anonymous1() { void validate() { new Anonymous2() { int calculate() { return Math.abs(Anonymous1.this.getValue()); //…
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
2
votes
0 answers

angular 2: when resolving promise in component via service 'this' is 'window'

I'm following the quickstart tutorial for Angular 2 (https://angular.io/docs/ts/latest/tutorial/toh-pt4.html#!#review-the-app-structure) and I got stuck in the Services chapter. This is my component: @Component({ selector: 'main', templateUrl:…
Carlos Romero
  • 698
  • 8
  • 18
2
votes
2 answers

substitute system.reflection for "this"

I have a method I am trying to make a little more easy to widely deploy. NHibernateISession.log4netLogFileEntry("DEBUG", "hello", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName); I would like to reduce the…
Steve
  • 905
  • 1
  • 8
  • 32
2
votes
5 answers

Scope of THIS keyword in Function?

Should 'this' in the following code not still refer to the DOM object selected by the MooTools selector? $$('div').addEvent('click', function() { var click1 = function(){$(this).setStyle('background', 'red');} click1(); });
Brownbay
  • 5,400
  • 3
  • 25
  • 30
2
votes
1 answer

Is $this a reference in PHP?

So, I'm just curious about something theoretical that is also practically useful. This question is also an immediate followup to this SO question. For the pseudo-variable "$this" in PHP, the manual has this to say: $this is a reference to the…
Brandon S.
  • 306
  • 1
  • 4
  • 14
2
votes
1 answer

Javascript function stored in array vs in a variable

In this example why does pushing the function reference onto the array not change the scope execution context of this whereas assigning the function reference to a new variable does change the scope execution context of this? (function() { …
seangwright
  • 17,245
  • 6
  • 42
  • 54
2
votes
2 answers

Getting incorrect values in my javascript project when passing function as callback on event handler

Here I have a just extracted my problematic part of code as it is an object with a clickInfo method that I want to use when testDivbtn in html is clicked​ var product = { jsondata:[ {title:"GearBox", price:80000}, …
user5907926
2
votes
4 answers

Is it true that using "this." before the parameters in c# uses more memory?

than just to call the parameter as it is?
Milana