I am following google chrome’s extension development guide here:
http://developer.chrome.com/extensions/getstarted.html
In line 41 of this file: http://sprunge.us/NFjZ
requestKittens: function() {
var req = new XMLHttpRequest();
req.open("GET", this.searchOnFlickr_, true);
req.onload = this.showPhotos_.bind(this);
req.send(null);
},
, they used the bind function like this
this.showPhotos_.bind(this); and without this binding, the example extension will not work. I tested in the showPhotos_ method and verified that “this” is just the kittenGenerator object. Since showPhotos_ is just a method of that object, this should be implicitly done, so why is this binding necessary anyway?
Note that google’s example will not work due to a spelling mistake in the popup.js file.
To correct it, change “kittensOnFlickr_” into “searchOnFlickr_”.
Without the binding
showPhotos_when called from req.onload, thethiswill bereqnotkittenGenerator.