I would like to understand how bind is used to change the scope of the function that is executed when the result is returned.
- What is the .bind executed on exactly?
- Why would the .bind affect the scope of code that is executed within the function?
Here is my code:
// scope is window...
console.log(this)
// with bind -> scope is window...
$.get("https://api.github.com/users/octocat/gists", function(result) {
var lastGist = result[0];
console.log(this)
}.bind(this));
// without bind -> scope is $.get
$.get("https://api.github.com/users/octocat/gists", function(result) {
var lastGist = result[0];
console.log(this)
});
I also tried the following code, but bind() did not seem to have an impact here:
var a = function(){console.log(this)}.bind(this)
var b = function(){console.log(this)}