Linked Questions

243 votes
19 answers
89k views

Unable to access React instance (this) inside event handler [duplicate]

I am writing a simple component in ES6 (with BabelJS), and functions this.setState is not working. Typical errors include something like Cannot read property 'setState' of undefined or this....
user3696212's user avatar
  • 3,409
185 votes
1 answer
432k views

Angular 2 - Using 'this' inside setTimeout [duplicate]

I have a function like so in my class showMessageSuccess(){ var that = this; this.messageSuccess = true; setTimeout(function(){ that.messageSuccess = false; },3000); } ...
user2085143's user avatar
  • 4,212
93 votes
7 answers
73k views

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

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 ...
Jimmy's user avatar
  • 36.6k
58 votes
7 answers
55k views

JavaScript Callback Scope [duplicate]

I'm having some trouble with plain old JavaScript (no frameworks) in referencing my object in a callback function. function foo(id) { this.dom = document.getElementById(id); this.bar = 5; ...
Chris MacDonald's user avatar
48 votes
8 answers
47k views

'this' in function inside prototype function [duplicate]

I basically have an object, extended with a function through its prototype. Inside that function, another function exists, however when using this in this nested function, it does not seem to refer to ...
pimvdb's user avatar
  • 154k
43 votes
4 answers
46k views

Variable inside setTimeout says it is undefined, but when outside it is defined [duplicate]

I have a class. I need to do some http work inside of a timeout. The problem I am faceing is the http variable inside the timeout keeps saying it is undefined. export class MyClass { http:Http: ...
user2924127's user avatar
  • 6,172
51 votes
1 answer
53k views

ES6 Class: access to 'this' with 'addEventListener' applied on method [duplicate]

In this es6 script, the click event don't works because sayHello method is called with this.elm (<div>) as this. how to associate a event to a method without loose the scope? class player{ ...
Yukulélé's user avatar
  • 16.7k
34 votes
1 answer
49k views

React. this.setState is not a function inside setTimeout [duplicate]

Current component has state.breaker value of false. When the scroll event is captured it looks at the state and if its false it does some stuff. I would like to have some kind of static delay before ...
volna's user avatar
  • 2,562
45 votes
2 answers
37k views

Referencing "this" inside setInterval/setTimeout within object prototype methods [duplicate]

Normally I'd assign an alternative "self" reference when referring to "this" within setInterval. Is it possible to accomplish something similar within the context of a prototype method? The following ...
Huck's user avatar
  • 507
30 votes
3 answers
29k views

Calling a class prototype method by a setInterval event [duplicate]

I have a simple javascript class. One method of this class sets up a timer using setInterval function. The method that I want to call every time the event fires is defined inside the same class. ...
Andres's user avatar
  • 3,404
20 votes
1 answer
49k views

this.setState is not a function [duplicate]

I have the following component, which maintains the state that gets updated when the event is fired on the an specific element and when the state is updated it is passed down as a prop to another ...
RRP's user avatar
  • 2,763
47 votes
1 answer
38k views

"This" within es6 class method [duplicate]

For some reason I'm getting weird values for "this" in my es6 class... 'use strict'; class Clicker { constructor(element) { this.count = 0; this.elem = element; this.elem....
Andrew Luhring's user avatar
15 votes
7 answers
15k views

How to pass "this" to window setInterval [duplicate]

Suppose I have a function a: function a() { this.b = 1; this.set = setInterval(function() {console.log(this.b);}, 200); } So when a.set() is called the anonymous function will be called. ...
NSF's user avatar
  • 2,519
27 votes
4 answers
5k views

Why doesn't ["A","B","C"].map(String.prototype.toLowerCase.call) work? [duplicate]

This, of course, returns what you would expect: ["A","B","C"].map(function (x) { return x.toLowerCase(); }); // --> ["a", "b", "c"] So does using String.prototype.toLowerCase.call: ["A","B","...
Greg Smith's user avatar
26 votes
1 answer
25k views

How to access 'this' inside a callback function in Typescript? [duplicate]

I am trying to set a variable declared at the beginning of the class (a boolean) to true once a callback is called, but I keep getting a TypeScript erorr. Here is the error: TypeError: Cannot set ...
Tristan C's user avatar
  • 613

15 30 50 per page
1
2 3 4 5
184