Skip to main content
Active reading [<https://en.wiktionary.org/wiki/another#Determiner>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

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

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 was created, but to an otheranother one.

I also tried to use an object method instead of an anonymous function:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};

but it exhibits the same problems.

How can I access the correct object?

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

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 was created but to an other one.

I also tried to use an object method instead of an anonymous function:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};

but it exhibits the same problems.

How can I access the correct object?

How to access the correct `this` inside a callback

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 was created, but to another one.

I also tried to use an object method instead of an anonymous function:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};

but it exhibits the same problems.

How can I access the correct object?

`this` is not a "context"
Link
T.J. Crowder
  • 1.1m
  • 196
  • 2k
  • 1.9k

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

I never find the dupe when using "context" as a search term :-/
Link
Bergi
  • 655.3k
  • 156
  • 998
  • 1.4k

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

"this" is not "context". ;-)
Link
RobG
  • 145.9k
  • 31
  • 176
  • 212
Loading
Question Protected by Samuel Liew
jsfiddle -> stacksnippets
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
edited tags
Link
Bergi
  • 655.3k
  • 156
  • 998
  • 1.4k
Loading
edited body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
extended the scope of the question a little bit, to include another common "problem use case"
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 61 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading