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

functional scope and "this" in javascript

I have this simple bit of code (where ko == knockout lib): $(function() { //var el = document.getElementById('foo'); //console.log(el); $("#foo").click (function() {console.log(this);}); }); // this == $("#foo") …
Musical Shore
  • 1,361
  • 3
  • 20
  • 41
2
votes
1 answer

this and *this difference

Following program output is correct but there comes a error when I use this in place of *this.Can anybody tell me what this and *this means #include using namespace std; class Test { private: static int count; public: …
user4304729
2
votes
1 answer

this operator in javascript

Suppose I have JavaScript code like myClass = function(){ function doSomething(){ alert(this); // this1 } } alert(this); //this2 What those two 'this' objects are refer for??
Muhit
  • 789
  • 1
  • 7
  • 17
2
votes
3 answers

Is using the same variable in a class ok to use in the constructor C++?

If I have a class: class className{ int i; public: className(int value); }; What is considered as the best practise for initializing the class variable 'i' from the constructor as per the below choices? 1) Use the actual field name with an…
Blueberry
  • 83
  • 6
2
votes
2 answers

JavaScript prototypes - technical interview

I had a JavaScript interview last wednesday, and I had trouble with one of the questions. Maybe you guys can give me hand with it? The question was: how would you go about this printing var a and s to the console, in camel case, with the help of a…
Johnny D.
  • 21
  • 1
2
votes
4 answers

on focus display just one element

I have a simple problem: I have a form:
Now when the textarea…
Schwesi
  • 4,753
  • 8
  • 37
  • 62
2
votes
0 answers

Node JS / Javascript: "this" is hidden in callback?

I want to access the variable "blocks" of my objects.. this is possible, if i use the name ot the function print, but is not possible if i use print as a callback. what am i doing wrong? how can i use the varibale "blocks" in…
manfred88
  • 21
  • 1
2
votes
1 answer

Scala.js jQuery ajax call example

I try to use scala.js, but I can not understand some of things. I shall be grateful for the help. import org.scalajs.dom.raw.HTMLElement import org.scalajs.jquery.{jQuery => JQ, JQueryAjaxSettings, JQueryXHR} def tableEvents() = JQ("td >…
2
votes
1 answer

Access JavaScript "this" from TypeScript

I'm currently working with the AlpacaJS library within my typescript project. This JavaScript/JQuery library is included in my Typescript class and wants me to pass some options in a json style object like so: this.options = { "collapsible":…
2
votes
3 answers

toggleClass with setInterval for current class (this)

I have a couple of boxes that have a shadow. I want the shadow to pulse on hover. The code below works but obviously makes all shadows to pulsate when I hover any one of them. I've tried to use this in the setInterval function in order to apply the…
Lenny
  • 446
  • 5
  • 21
2
votes
4 answers

JQuery using THIS as a .on [selector]

Hi, So i've got this code: $(document).on('click', ".buttonSelectAllOpt", function () { alert(this.attr("id")); }); and PHP: $iii=1 while([...]){ echo "Some…
user5306544
2
votes
1 answer

What is the value of "this" within an anonymous function in setTimeout below?

I've rewritten the underscore.js delay function to look like the below. It works after tinkering with using apply() but I don't fully understand what the "this" in the apply is pointing to in the anonymous function within setTimeout. _.delay =…
2
votes
1 answer

Javascript this undefined after function assignment

So I was reading the book Professional Javascript for Web developers and came across the following examples. var object = { name: "my Object", getName: function() { return this.name; } } The author then shows the following…
Yu Lin Chen
  • 571
  • 1
  • 6
  • 9
2
votes
1 answer

how to use this context in angularjs views?

well, I don't know if something like this is possible or not? so let's just ask and find out. in general, do we ever get able to use keyword this in angular…
azerafati
  • 18,215
  • 7
  • 67
  • 72
2
votes
2 answers

php oop don't know where/how to use it

Again here.... I'm trying to learn PHP OOP (like in the last post), but as much as I read, I can not find the use of classes, methods and properties very well. So at the moment I don't find any sense to all of this. So I've thought it would be good…
eve_mf
  • 795
  • 3
  • 12
  • 36
1 2 3
99
100