Created
February 18, 2014 15:03
-
-
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.
This file contains hidden or 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
| 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