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

Javascript [this] keyword binding with new

when reading the book You Don't Know JS: this & Object Prototypes I found this example about this binding: function foo(something) { this.a = something; } var obj1 = { foo: foo }; var obj2 = {}; obj1.foo( 2 ); console.log( obj1.a ); //…
Tuximo
  • 57
  • 11
2
votes
1 answer

How to curry jQuery methods -- which `this` value will pass the jQuery object?

If I want to create a jQuery function using Function.prototype.bind, i.e. to the need for a wrapping function, which this value do I supply? The following trivial example does not seem to work: // e.g.: $.fn.outerHeight() with argument true gets…
lunelson
  • 561
  • 6
  • 11
2
votes
7 answers

this refers to current object. But can't understand the below behaviour

class Person { string name; public Person(string name) { this.name = name; } public void method() { Person gupta = new Person("James"); // Current Object Console.WriteLine(this.name); Person…
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
2
votes
6 answers

Correct use of this. in a class constructor

I was browsing some documentation for a physics library for XNA and noticed an example someone had used for creating a class for a Car. This is a pretty simple example: Class Car { private float gravity; private float maxSpeed; public…
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
2
votes
1 answer

How to access 'this' from an object with in an object

I have a js object like this: let service = function () { this.a = 100; } service.prototype.func = function() { console.log(this.a) } service.prototype.func2 = function () { console.log('func2') this.func(); } service.prototype.obj = { …
Deo Leung
  • 848
  • 9
  • 9
2
votes
1 answer

what 'this' keyword is referring to within the context of DataStructureIterator iterator = this.new EvenIterator();

I'm new to Java and code below is from Java tutorial Oracle. I'm puzzled by two questions 1) Could someone please tell me what the "this" keyword is referring to within the context of DataStructureIterator iterator = this.new EvenIterator(); I…
Thor
  • 9,638
  • 15
  • 62
  • 137
2
votes
1 answer

Javascript/jQuery Event Handler Scope inside object using keyword this

I've been trying for days to figure this out. I have read many questions on SO as well as googled it many different ways and read/attempted everything I found. Nothing I have found so far has worked for me and I have rewritten my code a million…
Tech Savant
  • 3,686
  • 1
  • 19
  • 39
2
votes
2 answers

Understanding React Behind The Scene

Im trying to get my head around 'Components' in React. I have a few questions so I thoughts I'd SO community is the best place to ask them. 1 - When we do this: var foo = React.createClass ({ ... }); Console.log tells me that this is a…
Kayote
  • 14,579
  • 25
  • 85
  • 144
2
votes
1 answer

C# How to hack/fix 'this' keyword into a struct?

Apparently you cannot use the keyword "this" in a method in a struct. Note that in the below example I do not explicitly type "this" but it is implied when I reference the properties "X" or "Y". My struct: public struct Coord { public int X; …
2
votes
3 answers

Scope and OR operator in Javascript

I walked on something weird while doing some JS code today. I wanted to execute an object's method if the property existed, and some other function if it didn't. Feeling a little bit fancy, I wrote something like: var obj = { method:…
mikaël
  • 453
  • 3
  • 8
2
votes
1 answer

Reactjs - Using "this" in components - should it be avoided at all times?

In a nutshell After using React for a few months - the idea of using this to keep track of changing variables in a component has come to feel like snorkeling in the North Pole - no one should do it,ever. But with Leaflet that is kind of what…
Nick Pineda
  • 6,354
  • 11
  • 46
  • 66
2
votes
1 answer

jQuery, $(this).parent() and blur()

I cannot get the blur() function in the following to work: $('.newselect').focus(function(){ $(this).parent().append('
'); }); $('.newselect').blur(function(){ …
Phil
  • 225
  • 4
  • 13
2
votes
4 answers

JavaScript - Best way to always access a specific Object

If I create an Object as follows: window.Something = { X: function() { // statements... } }; What is the best way to access this object without using this? Example: If I call the X property by…
2
votes
1 answer

How do I select the correct 'this' selector?

JSFIDDLE: http://jsfiddle.net/nqsfoqzz/10/ I'm having trouble figuring out what the this selector is in my code below. If I call the function removeAndClose(elem); as shown near the end of the click event (currently commented out), the value of…
Pegues
  • 1,693
  • 2
  • 21
  • 38
2
votes
2 answers

jQuery plugin: how to pass element reference to callback function?

TL;DR: I have jQuery.myPlugin $.fn.myPlugin = function(param, callback){ //...which does some operations to each member of collection //obtained by $('.someclass').myPlugin() //that are represented with this variable } how to pass this…
Miloš Đakonović
  • 3,751
  • 5
  • 35
  • 55