I understand bind
creates a new function with the this
value replaced. The example
function edit(req, res) {
db.User.findById('ABCD', (function(err, user){
this.foo(user);
}).bind(this));
};
from Maintaining the reference to "this" in Javascript when using callbacks and closures
has the callback function providing its own parameters.
I am using http.min.js
, which handles callbacks like this:
http.get({
url: "http://localhost:8080/player/status/" + name,
onload: function() {
console.log(this.responseText);
notice the lack of this passed in - this.responseText
will give me a string I can parse for JSON
.
What is the simplest way to maintain the "this" from http.min.js
callback and bind to my callback function?
I like the syntax of
http.get({
url: "http://localhost:8080/player/status/" + name,
onload: this.handlePlayerInfo.bind(this);
and if possible want to use that, instead of using var that
.
Using library: https://github.com/quantumpotato/http.min.js/blob/master/http.min.js