Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- BIND
- a function with the this value set explicitly
- We can borrow the function
- **/
- var Button = function(content) {
- this.content = content;
- };
- Button.prototype.click = function() {
- console.log(this.content + ' clicked');
- }
- var myButton = new Button('OK');
- myButton.click();
- var looseClick = myButton.click;
- looseClick();
- var boundClick = myButton.click.bind(myButton);
- boundClick();
- // Example showing binding some parameters
- var sum = function(a, b) {
- return a + b + this.c;
- };
- //We can pass this object
- var add5 = sum.bind({c:10});
- console.log(add5(5,6));
- var sum = function() {
- return a + b + this.c;
- };
- //We can pass arguments
- //bind(this, argument1, argument2....)
- var add5 = sum.bind({c:10}, 5, 6);
- console.log(add5());
Add Comment
Please, Sign In to add comment