Incident Diagnosis in a Symptom-Based World
runbooks.gitlab.com2 ポイント投稿者 igorw0 コメント
var obj = { f: function () { return this; } };
// returns the object
obj.f()
// returns window
var f = obj.f;
f();
What is happening here is that the setTimeout() calls the lambda at some later point, and it's no longer in the context of the object. var obj = (function () {
var that = {};
that.doSomethingLater = function () {
setTimeout(function () { console.log(that); }, 1000);
};
return that;
})();
obj.doSomethingLater();
An article on the subject (that also talks about call and apply): http://www.robertsosinski.com/2009/04/28/binding-scope-in-ja...