this

How to access the correct `this` inside a callback?

感情迁移 提交于 2019-12-13 20:26:31
问题 I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); }); } // Mock transport object var transport = { on: function(event, callback) { setTimeout(callback, 1000); } }; // called as var obj = new MyConstructor('foo', transport); However, I'm not able to access the data property of the created object inside the callback. It looks like this does not refer to the object that

jQuery $(this) problems with $.post()

核能气质少年 提交于 2019-12-13 19:19:42
问题 So here's my code for when a user clicks a follow button: $('.follow_btn').click(function() { $(this).html('<img src = "../assets/style_images/loading.gif">'); var userId = $(this).attr('id'); $.post('../assets/scripts/ajax_follow_parse.php', { userId: userId }, function(data) { $(this).html(data); }); }); It is quite happy to replace it with the loading gif as shown on line 2. But when it returns the data and replace it with the data returned on line 4. How can I fix this? 回答1: Assign $(this

Passing THIS into an Immediately-Invoked Function Expression

六眼飞鱼酱① 提交于 2019-12-13 18:56:17
问题 Is there any way to pass this into an Immediately-Invoked Function Expression, without resolving to var that = this (which isn't applicable on some cases)? Tried the following but with no luck: (function(that) { console.log(that); })(this) 回答1: You might be able to use call or apply for this purpose. For example: (function() { console.log(this); // whatever that was specified in the "call" method }).call(this); 回答2: (function(that) { console.log(that); })(this); The code should work, make

“Leaking this” from a Design Standpoint

一世执手 提交于 2019-12-13 15:47:16
问题 Warning: Leaking "this" in constructor I keep running into this, and I have a nagging feeling that it's because my design is wrong or not optimal. I understand that this warning is bringing to my attention the fact that I am allowing access to an object that is potentially not fully initialized. Let's say that I need a Frame that HAS and requires a List (Frame(List list)). In List, I might want to do something such as add(). In order to make sure Frame knows as little about List as possible

Does the C++ compiler generate “this” pointer for all member methods, or only for those who reference members?

*爱你&永不变心* 提交于 2019-12-13 15:20:05
问题 Does the C++ compiler generate the hidden "this" pointer for all member methods, or only for those who reference members? 回答1: Short answer: yes. Longer answer: Well, if it's not using ANY member variables, why would it be a member function in the first place [and not a static member function]. (Yes, there may be cases where an interface provides a member method that isn't doing anything with any member content, since it's just doing something like printing an error message for calling this

Using 'this' as a parameter to a method call in a constructor

感情迁移 提交于 2019-12-13 13:04:08
问题 I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this.setTopWorldAgent(this, "Top_World_Agent", true); } I'm getting a null pointer exception here in the method call. It appears to be because I'm using 'this' as an argument in the setTopWorldAgent method. By removing this method call everything appears fine. Why does this happen? Has anyone else experienced this? 回答1: You can pass this to methods, but setTopWorldAgent() cannot be abstract. You can't make

jquery “this” binding issue on event handler (equivalent of bindAsEventListener in prototype)

狂风中的少年 提交于 2019-12-13 12:02:11
问题 In jquery an event hadler's binding is the event generating DOM element (this points to the dom element). In prototype to change the binding of an event handler one can use the bindAsEventListener function; How can I access both the instance and the DOM element from a event handler? Similar to How can I bind an event handler to an instance in JQuery? function Car(){ this.km = 0; $("#sprint").click(this.drive); //setup event handler } // event handler // in it I need to access both the clicked

Titanium Javascript: “that.” does not work

断了今生、忘了曾经 提交于 2019-12-13 11:27:50
问题 Neither this nor that works. Does anyone know what is going on?? Edit: qwerty is simply called as "qwerty();" when in other pieces of code. It is supposed to be indepedent. Edit: I realize what is wrong. The problem lies with the i... function qwerty () { ..... for loop that changes i ...... var that = this; this.chara[i] = createlabel..... this.chara[i].addEventListener('click', function(e) { var j = e.source.id; alert("hello word"); alert(this.chara[j].width); // I get the error here });

calling a method from an onClick listener [duplicate]

可紊 提交于 2019-12-13 10:03:33
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Accessing the containing class of an inner class in Java I know this has certainly been answered before, but I have been working on this for two straight nights and I either don't understand or I have messed something up badly. I am trying to call a method with a button. My method is only going to copy and paste so it's not opening another activity. package com.example.copypastetest; import android.app.Activity;

Access List<List<String>> using this keyword [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-13 09:47:08
问题 This question already has an answer here : How to get List<List<String>> with this keyword? (1 answer) Closed 2 years ago . Lets assume that I store values to List A, List B & List C through for loop. List A,B,C values is added by the following way: for (int j = 0; j <5; j++) { list_A.add("Value"+i); list_B.add("Value"+i); list_C.add("Value"+i); } To access the List out of the method I'm using this keyword by the following way: private CharSequence ListA[]; // declaration this.ListA= list_A