What are the pros and cons of using a reference object versus using JavaScript's .bind()? Are there some cases (real world or theoretical) where one might be optimal?
Here is a simple example...
var obj = {
message : "Hello World"
,buttonElt : document.getElementById('myButton')
,init : function(){
// A --- Using a reference object so the function can reference this obj
var o = this;
buttonElt.addEventListener("click", function(){
alert(o.message);
}, false);
// B --- using .bind to send this obj
buttonElt.addEventListener("click", function(){
alert(this.message);
}.bind(this), false);
}
}
obj.init();