Skip to content

Instantly share code, notes, and snippets.

@slopeofhope81
Created February 18, 2014 15:03
Show Gist options
  • Select an option

  • Save slopeofhope81/9072649 to your computer and use it in GitHub Desktop.

Select an option

Save slopeofhope81/9072649 to your computer and use it in GitHub Desktop.
Now I understand the power of bind method how it will always run in the given context.
For example, take a look at the first example below:
1)(function(){
var cart={
item: "product1",
price: 35.50,
quantity: 2,
buying: function(){
alert("you bought"+this.item);
}
};
//cart.buying=cart.buying.bind(cart)
var text=document.getElementById("text");
text.addEventListener("click",cart.buying);
})();
If you run this without the bind method, it will NOT print out the value of a property named item. Instead it will print out undefined because it is only passing a reference to the function. !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment