this

this inside function

My question is: function Foo() { this.foo = "bar"; // <- What is "this" here? } From what I can tell it depends on how Foo is used, i.e. as a constructor or as a function. What can this be in different circumstances? ...

Get function caller scope

Is it possible to extract a functions caller scope? I have this example: var myObject = { id: 1, printId: function() { var myId = function() { return this.id; } console.log(myId()); } } myObject.printId(); // prints 'undefined' The example above prints out undefined, because 'this' see...

Confusion with "this" object in JavaScript anonymous functions.

Hi I have following JavaScript code that I am trying to run. My aim is to grasp the meaning of this in different scopes and different types of invocations in JavaScript. If you look in code below: I have a inner anonymous function, which is getting assigned to innerStuff variable. In that anonymous function as such this points to windo...

Preserving a reference to "this" in JavaScript prototype functions

I'm just getting into using prototypal JavaScript and I'm having trouble figuring out how to preserve a this reference to the main object from inside a prototype function when the scope changes. Let me illustrate what I mean (I'm using jQuery here): MyClass = function() { this.element = $('#element'); this.myValue = 'something'; ...

Link click function of parent object to redirect to href

I'm feeling so dumb asking this question, but I can't figure out a clean way to write this... Heres the HTML <li> <a class='link_parent' href='#'>Test</a> </li> I want the click function of the parent LI to redirect the href of the a with .link_parent... so... $('a.link_parent').each(function() { $(this).parent().click(function()...

jquery passing $(this) to other functions

High! What i want to do is the following: i have a table with a onclick attached to a link that resides in the table of an even row. every odd row is hidden. on a click on that link, the odd row is shown and data is loaded into that row. Works fine Now what i want to do is that whenever that process is done, i want to attach a new clic...

How to reassign `this` pointer inside object member function?

I have an interesting question about C++ pointers. You probably will think that I have to change my design, and avoid doing what I am doing, and you are probably right. But let's assume that I have a good reason to do it my way. So this is the situation. I have a C++ class TestClass, and I have a pointer A of this type: TestClass* A =...

C++ 'this' and operator overloading

I was wondering how can you use operators along with 'this'. To put an example: class grd : clm { inline int &operator()(int x, int y) { return g[x][y]; } /* blah blah blah */ }; grd grd::crop(int cx1, int cy1, int cx2, int cy2) { grd ngrd(abs(cx1-cx2), abs(cy1-cy2)); int i, j; // for (i = cx1; i < cx2; i++) ...

Is always prefixing (auto)properties with the this-keyword considered a good practice?

Ever since I found out about auto properties, I try to use them everywhere. Before there would always be a private member for every property I had that I would use inside the class. Now this is replaced by the auto property. I use the property inside my class in ways I normally would use a normal member field. The problem is that the pro...

Maintain reference to this in jQuery

I'm converting a bunch of hyperlinks to make simple GET requests using jQuery. I want to maintain the reference to this within the Ajax call, do i need to be using bind/live/something else? $(document).ready(function(){ $(".mylink").click(function(){ var url = $(this).attr('href'); $.get(url,function(data){ ...

How to explain 'this' keyword in a best and simple way?

I am using 'this' keyword for a long time. But when someone asks me to explain it, I am confused that how to explain it. I know that I can use this in a method of class to access any variable and method of the same class. class MyClass{ function MyMethod1(){ echo "Hello World"; } function MyMethod2(){ ...

'this' keyword refers to what object within an a function inside another function?

Hi, Basically I am trying to understand and learn the 'this' keyword's working principle in JavaScript. As far as I understand 'this' refers to the object (function) that it is inside at that moment. So, by believing this, I wanted to test the output of the simple code below: <body> <input type="button" value="Add Age" onclick="Ou...

jQuery: $("#" + $(this).attr('id') + "_div")

I'm trying to modify a name-related element. I'm taking values from a drop-down and displaying them in a div. Their ids are: selected_terms selected_terms_div I have a number of these pairs and I was hoping to write generalized code instead of hard-coding it (I figure it'll be useful later). This code isn't working: $("#" + $(this).at...

Dynamically picking variable from a class

I'm trying to get data from a class in php5, where the data in the class is private and the calling function is requesting a piece of data from the class. I want to be able to gain that specific piece of data from the private variables without using a case statement. I want to do something to the effect of: public function get_data($f...

How does jQuery hijack "this"?

I'm just curious to know how jQuery is able to hijack the 'this' keyword in Javascript. From the book I'm reading: "Javascript the Definitive Guide" it states that "this" is a keyword and you cannot alter it like you can with an identifier. Now, say you are in your own object constructor and you make a call to some jQuery code, how i...

JavaScript/JQuery: use $(this) in a variable-name

Hi! I'm writing a jquery-plugin, that changes a css-value of certain elements on certain user-actions. On other actions the css-value should be reseted to their initial value. As I found no way to get the initial css-values back, I just created an array that stores all initial values in the beginning. I did this with: var initialCSSV...

Why is the 'this' keyword not a reference type in C++

Possible Duplicates: Why this is a pointer and not a reference? SAFE Pointer to a pointer (well reference to a reference) in C# The this keyword in C++ gets a pointer to the object I currently am. My question is why is the type of this a pointer type and not a reference type. Are there any conditions under which the this ke...

Refering to the class itself from within a class mehod in Objective C

I hope i did not skip this part in the ObjC manual but is it possible to refer to a class from within one of its class methods? Like in PHP you would use "this" to refer to the current instance, while "self" refers to the instance's class the ObjC equivalent of "this" would be "self", so what would be the ObjC equivalent of PHP's "self",...

jQuery first child of "this"

I'm trying to pass "this" from a clicked span to a jQuery function that can then execute jQuery on that clicked element's first child. Can't seem to get it right... <p onclick="toggleSection($(this));"><span class="redClass"></span></p> Javascript: function toggleSection(element) { element.toggleClass("redClass"); } How do I refe...

Give the JavaScript the button's id.

In my JavaScript code, I have to make a line like this (I use smarty template engine, that is the literal stuff). <script language="javascript" type="text/javascript"> function ajaxTextKill() { // ...etc. ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+IWANTMYIDHERE+"&p={$smarty.get.page}&c={$smarty...