this

Javascript setInterval and `this` solution

随声附和 提交于 2019-11-26 00:12:46
问题 I need to access this from my setInterval handler prefs: null, startup : function() { // init prefs ... this.retrieve_rate(); this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL); }, retrieve_rate : function() { var ajax = null; ajax = new XMLHttpRequest(); ajax.open(\'GET\', \'http://xyz.com\', true); ajax.onload = function() { // access prefs here } } How can I access this.prefs in ajax.onload ? 回答1: The setInterval line should look like this:- this.intervalID = setInterval(

Pass correct “this” context to setTimeout callback? [duplicate]

痴心易碎 提交于 2019-11-25 23:57:30
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . How do I pass context into setTimeout ? I want to call this.tip.destroy() if this.options.destroyOnHide after 1000 ms. How can I do that? if (this.options.destroyOnHide) { setTimeout(function() { this.tip.destroy() }, 1000); } When I try the above, this refers to the window. 回答1: EDIT: In summary, back in 2010 when this question was asked the most common way to solve

How does the “this” keyword work?

风格不统一 提交于 2019-11-25 23:55:11
问题 I have noticed that there doesn\'t appear to be a clear explanation of what the this keyword is and how it is correctly (and incorrectly) used in JavaScript on the Stack Overflow site. I have witnessed some very strange behaviour with it and have failed to understand why it has occurred. How does this work and when should it be used? 回答1: I recommend reading Mike West's article Scope in JavaScript (mirror) first. It is an excellent, friendly introduction to the concepts of this and scope

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

我的未来我决定 提交于 2019-11-25 23:55:07
问题 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

Difference between $(this) and event.target?

余生长醉 提交于 2019-11-25 23:50:54
I'm new to jQuery, and was making tabbed panels, following the tutorial in JavaScript and jQuery : The Missing Manual , there's that first line when the author does this : var target = $(this); But i tried to do it that way var target = evt.target; and i got that error : Uncaught TypeError: Object http://localhost/tabbedPanels/#panel1 has no method 'attr' And when i changed evt.target back to $(this) , it worked like a charm. I want to know what's the difference between $(this) and evt.target ? Here's my code in case you needed it : index.html : <!DOCTYPE html> <html> <head> <title>Tabbed

React: “this” is undefined inside a component function

南笙酒味 提交于 2019-11-25 23:49:24
问题 class PlayerControls extends React.Component { constructor(props) { super(props) this.state = { loopActive: false, shuffleActive: false, } } render() { var shuffleClassName = this.state.toggleActive ? \"player-control-icon active\" : \"player-control-icon\" return ( <div className=\"player-controls\"> <FontAwesome className=\"player-control-icon\" name=\'refresh\' onClick={this.onToggleLoop} spin={this.state.loopActive} /> <FontAwesome className={shuffleClassName} name=\'random\' onClick=

Preserving a reference to “this” in JavaScript prototype functions [duplicate]

醉酒当歌 提交于 2019-11-25 23:22:12
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . 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\'; // some more code } MyClass

What does the variable $this mean in PHP?

↘锁芯ラ 提交于 2019-11-25 23:14:39
问题 I see the variable $this in PHP all the time and I have no idea what it\'s used for. I\'ve never personally used it. Can someone tell me how the variable $this works in PHP? 回答1: It's a reference to the current object, it's most commonly used in object oriented code. Reference: http://www.php.net/manual/en/language.oop5.basic.php Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html Example: <?php class Person { public $name; function __construct( $name ) { $this-

How to get the children of the $(this) selector?

北城余情 提交于 2019-11-25 23:05:21
问题 I have a layout similar to this: <div id=\"...\"><img src=\"...\"></div> and would like to use a jQuery selector to select the child img inside the div on click. To get the div , I\'ve got this selector: $(this) How can I get the child img using a selector? 回答1: The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection. jQuery("img", this); Which is the same as using .find() like this: jQuery(this).find("img"); If the imgs you

VueJS: why is “this” undefined?

江枫思渺然 提交于 2019-11-25 22:46:30
问题 I\'m creating a component with Vue.js. When I reference this in any of the the lifecycle hooks ( created , mounted , updated , etc.) it evaluates to undefined : mounted: () => { console.log(this); // logs \"undefined\" }, The same thing is also happening inside my computed properties: computed: { foo: () => { return this.bar + 1; } } I get the following error: Uncaught TypeError: Cannot read property \'bar\' of undefined Why is this evaluating to undefined in these cases? 回答1: Both of those