Sunday, 12 March 2017

Use this in a callback

i'm developing a little script in NodeJS and i created an object. I would like to use the data of the object in my object function. In this object function another function gets called and i'm using the callback of it.

But if i try to use 'this' in the callback its not my object in there.

I read some other threads and i found the function 'bind'. But if i call the bind function, my script crashes and prints

"TypeError: this.connection.on(...).bind is not a function"

My code looks like this:

obj.prototype.func = function() {
this.foo = RandomNPM;
foo.bar("", function(){
    console.log(this);
}).bind(this);

}



via Julian Stone

No comments:

Post a Comment