Skip to content

Instantly share code, notes, and snippets.

@optimistanoop
Created January 13, 2017 19:15
Show Gist options
  • Save optimistanoop/6ae66e621284eb63c0d444c4100bdba0 to your computer and use it in GitHub Desktop.
Save optimistanoop/6ae66e621284eb63c0d444c4100bdba0 to your computer and use it in GitHub Desktop.
this in Js callbacks
var fn = function(a,b){
console.log(this, a ,b);
}
setTimeout(fn, 100); // global, undefined , undefinded , assuming fn is called from the implimentation of
// setTimeout like cb(); "this" in callbacks completely depends on the way they are called.
// as "this" in callbacks completely depends on the way they are called, still we can be sure about "this" in our callbacks.
var customThis = " this";
setTimeout(function(){
fn.call(customThis,1,2);
}, 100);
// logs "this", 1,2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment