Created
January 13, 2017 19:15
-
-
Save optimistanoop/6ae66e621284eb63c0d444c4100bdba0 to your computer and use it in GitHub Desktop.
this in Js callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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