this

Using this.setState in a callback

有些话、适合烂在心里 提交于 2019-11-28 14:05:19
I have the following code getting a twitter timeline in a react component: componentWillMount: function() { twitter.get('statuses/user_timeline', function(error, data) { this.setState({tweets: data}) }); } But I can't set the state there, because this isn't set to the component in that callback function. How can I set the state within that callback? n.b. console.log(data) instead of this.setState works fine, which is why I suspect the problem is with the this variable. You can set this with .bind method like this, and call twitter.get in componentDidMount as in this example componentDidMount:

Javascript OOP - lost this in asynchronous callback

你离开我真会死。 提交于 2019-11-28 13:35:30
I have problem which still bothers me on js oop - I'm sure I'm doing it bad, but I cant get how to do it right. For example, I have this code Auth.prototype.auth = function () { var request = new XMLHttpRequest(); request.open('GET', this.getAuthServerURL() + '/token', true); request.send(); request.onloadend = function () { var response = JSON.parse(request.responseText); console.log(response); if(response.result == 'found') { var token = response.token; this.setToken(token); this.isSigned = true; } else { console.log('Not logged yet.'); } } } The problem is that I cant access to function

Is `new (this) MyClass();` undefined behaviour after directly calling the destructor?

心不动则不痛 提交于 2019-11-28 13:27:44
In this question of mine , @DeadMG says that reinitializing a class through the this pointer is undefined behaviour. Is there any mentioning thereof in the standard somewhere? Example: #include <iostream> class X{ int _i; public: X() : _i(0) { std::cout << "X()\n"; } X(int i) : _i(i) { std::cout << "X(int)\n"; } ~X(){ std::cout << "~X()\n"; } void foo(){ this->~X(); new (this) X(5); } void print_i(){ std::cout << _i << "\n"; } }; int main(){ X x; x.foo(); // mock random stack noise int noise[20]; x.print_i(); } Example output at Ideone (I know that UB can also be "seemingly correct behaviour")

PHP Fatal error: Cannot use $this as parameter

点点圈 提交于 2019-11-28 13:06:26
I've the following PHP method which is part of the codebase which was working fine: <?php class HooksTest extends DrupalTestCase { public function testPageAlterIsLoggedIn() { $this->drupal->shouldReceive('userIsLoggedIn') ->once() ->andReturn(TRUE); $this->drupal->shouldReceive('drupalPageIsCacheable') ->once() ->andReturnUsing(function ($this) { return $this; }); $page = []; $cacheable = $this->object->pageAlter($page); $this->assertFalse($cacheable); } } The code was passing all the CI tests before (using phpunit ). However now when I'm invoking the file via php HooksTest.php , I've got the

Any way to define ALL variables within function as property of this?

孤人 提交于 2019-11-28 13:03:28
I know this may sound a little absurd, but I'm looking for a way to define every variable within a function as a property of this . I'm looking for any hack, any way possible to be able to have some way to track variables within a function (i.e. add them to the this object) without having to actually preface every single variable definition with this. . Is there a way? Is this possible with Proxy ? function () { // declare a variable var hello = 'hi' return this } let {hello} = function() console.log(hello) // hi For example this works: function hi () { this.hello = true; return this } hi.bind

Passing $(this) to a callback in jQuery?

房东的猫 提交于 2019-11-28 12:44:55
I have two effects that I want to run one after another, but I can't work out how to pass $(this) to the callback from the first effect. This is what I have at the moment: if($(this).hasClass('flag')) { $('someElement').slideUp(); $(this).slideDown(); } But what I want is for the slideDown to run after the slideUp is complete, ie. if($(this).hasClass('flag')) { $('someElement').slideUp(function() { $(this).slideDown(); }); } Except $(this) by that stage now refers to someElement instead and so doesn't work as planned. How do I refer to my original element.flag . I tried using $this = $(this)

java this keyword

走远了吗. 提交于 2019-11-28 12:42:52
I have read that in Java you don't have to explicitly bind the this keyword to object, it is done by interpreter. It is opposite to Javascript where you always have to know the value of this . But where is this in Java is pointing - to class or object ? Or does it vary ? This question is a part of my attempt to understand basic OO concepts and design patterns so I can apply them to Javascript. Thank you. The Java language specification states: When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12)

Usage of “this” in destructor

好久不见. 提交于 2019-11-28 12:04:46
Is it valid to call some function in destructor with this argument? Function does not store pointer, but assume full-functional object. this is still valid in the destructor. However, you need bear in mind that virtual functions no longer work properly as you might expect once the object is being destroyed; see e.g. Never Call Virtual Functions during Construction or Destruction . Essentially, the dynamic type of the object is modified as each destructor completes. In one word: YES. It's fully valid to use this in the D`TOR 来源: https://stackoverflow.com/questions/10979250/usage-of-this-in

The use of “this” in Java

瘦欲@ 提交于 2019-11-28 10:16:15
If I write the following class: public class Example { int j; int k; public Example(int j, int k) { j = j; k = k; } public static void main(String[] args) { Example exm = new Example(1,2); System.out.println(exm.j); System.out.println(exm.k); } } The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write: this.j = j; this.k = k; But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand

'this' object can't be accessed in private JavaScript functions without a hack?

百般思念 提交于 2019-11-28 10:14:42
I was working on a project for a while, trying to figure out what I was doing wrong, when I finally narrowed "the bug" down to the fact that the below code doesn't work as I expected: function Alpha() { this.onion = 'onion'; function Beta() { alert(this.onion); } Beta(); } alpha1 = new Alpha(); // Alerts 'undefined' However, if I change the code to this: function Alpha() { var self = this; this.onion = 'onion'; function Beta() { alert(self.onion); } Beta(); } alpha1 = new Alpha(); // Alerts 'onion' it works like I would expect. After wasting a large portion of my life, can anyone explain why