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

this.getView() is not working - SAPUI5

I've been trying to use this.getView() on my controllers a couple of times but I'm always getting getView is not a function error on my console. Is there any "trick" or something that I can do to make this work? Heres's an example of how I'm using…
Eva Dias
  • 1,709
  • 9
  • 36
  • 67
2
votes
1 answer

How to add `this` qualifier to Java fields in Eclipse?

Is there any functionality in Eclipse to refactor my code such that unqualified field accesses gain the this qualifier? For example, I had been writing some of my code in static methods but would now like to change them to non-static methods. I…
Kyzderp
  • 326
  • 2
  • 9
2
votes
1 answer

"this" is unavailable when directly initializing class member variable

Why does the compiler give an error about this being unavailable that when initializing an instance variable when defining it? Keyword 'this' is not available in the current context The constructor initialization is fine. I understand the…
Jason W
  • 13,026
  • 3
  • 31
  • 62
2
votes
2 answers

Understanding "this" within anonymous function in JavaScript

In this post, lots of answers are there discussing the this keyword in JavaScript. However, I am still confuse this in the anonymous function as following // MyModule.js 'use strict'; (function(handler) { // export methods handler.B = B; …
zangw
  • 43,869
  • 19
  • 177
  • 214
2
votes
0 answers

Javascript. Using a class to refer a transition in another

I am making an Image slider. It zooms on hover and navigates through pictures in respect of an Input button named .btnNext. The Images are held in an array named .myImages. Everything is working fine so far though one problem has persisted. After…
Star's Studio
  • 97
  • 1
  • 10
2
votes
3 answers

When would casting 'this' be required?

Are there any situations in Java that would require you to explicitly cast this to another type other than the current class? class SomeClass { void foo() { SomeOtherClass s = (SomeOtherClass) this; } } EDIT: All answers refer…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
2
votes
1 answer

How to make "this" keyword refer to current instance, if used from a nested object's method?

In JavaScript, if I attach a function to an object's prototype, within that function, this refers to the current instance of the object. For example: function RageFest(inquiry) { this.inquiry = inquiry; } RageFest.prototype.myFunction0 =…
tinybike
  • 562
  • 2
  • 13
2
votes
2 answers

AngularJS - the use of this vs $scope in a POST request

I have read several posts on the use of this instead of $scope, and vice versa, but being fairly new to javascript in general, I feel like I'm still missing something. Below is a code example where I'm doing a POST request. But by the time I get…
Nilzone-
  • 2,766
  • 6
  • 35
  • 71
2
votes
3 answers

Get the second child of a jQuery element

This question might be duplicated, but I haven't found anything helpful. Here's my snippet: $("table tbody tr").hover( function() { var secondCell = $(this).children[1].textContent; //secondCell.someCode }, function()…
PepsiGam3r
  • 302
  • 1
  • 4
  • 10
2
votes
2 answers

Example from book secrets of javascript ninja

Starting to read javascript ninja book and I really do not understand why the word 'this' is needed in below example. I tried it w/out it and code do not run. What purpose does the 'this' serve in below context? I think I understand the 'this'(or…
user3502374
  • 781
  • 1
  • 4
  • 12
2
votes
2 answers

Returning `this` in a super method of inherited class

let's say i have class A and class B which extends A, here are the classes: A: public class A { public int x; public static int y; public A(int x, int y) { this.x = x; this.y = y; } public int getX() { return x;…
argamanza
  • 1,122
  • 3
  • 14
  • 36
2
votes
1 answer

Java 8 Explicit 'this' Parameter

I recently heard that it is possible since Java 8 to define an explicit parameter called this in instance methods, like this: public class Test { public void test(Test this, int i) { System.out.println(i); } } What is the use for this kind of…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
2
votes
2 answers

trying to extract a string from a js this keyword

I'm trying to execute javascript after a link is clicked before loading the link, using this code: $('body').on("click", 'a', function (evt) { evt.preventDefault(); console.log(this); …
phaseform
  • 33
  • 4
2
votes
1 answer

static variables within javascript functions and how it works under the hood

I'm having real trouble getting my head around some fundamental concepts in JavaScript so am hoping someone can give me a good explanation of what is happening under the hood in these examples. CASE 1: In this case I use the 'this' keyword in a…
Sam Redway
  • 7,605
  • 2
  • 27
  • 41
2
votes
2 answers

Javascript call method

Why the following code will output 1 ? function func(){ alert(this) } var i = 1; func.call(i);
J.c
  • 23
  • 3
1 2 3
99
100