Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. /**
  2. BIND
  3. a function with the this value set explicitly
  4. We can borrow the function
  5. **/
  6.  
  7. var Button = function(content) {
  8. this.content = content;
  9. };
  10. Button.prototype.click = function() {
  11. console.log(this.content + ' clicked');
  12. }
  13.  
  14. var myButton = new Button('OK');
  15. myButton.click();
  16.  
  17. var looseClick = myButton.click;
  18. looseClick();
  19.  
  20. var boundClick = myButton.click.bind(myButton);
  21. boundClick();
  22.  
  23. // Example showing binding some parameters
  24. var sum = function(a, b) {
  25. return a + b + this.c;
  26. };
  27.  
  28. //We can pass this object
  29. var add5 = sum.bind({c:10});
  30. console.log(add5(5,6));
  31.  
  32. var sum = function() {
  33. return a + b + this.c;
  34. };
  35. //We can pass arguments
  36. //bind(this, argument1, argument2....)
  37. var add5 = sum.bind({c:10}, 5, 6);
  38. console.log(add5());
Add Comment
Please, Sign In to add comment