![]() |
what is bind() method in javascript? what is bind() method in javascript? |
Re: what is bind() method in javascript? Quote:
Quote:
Quote:
|
Re: what is bind() method in javascript? It will bound ' this' value to the first argument of bind function. If you bind some object to another, this.var will become var of the binded object. |
Re: what is bind() method in javascript? The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs. eg. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").bind("click", function(){ alert("The paragraph was clicked."); }); }); </script> </head> <body> <p>Click me!</p> </body> </html> |
Re: what is bind() method in javascript? bind value base on tha parameters, its like a function sysntex of bind method fun.bind(thisArg[, arg1[, arg2[, ...]]]) where arg1 , arg 2 is a variable you put a values of those variables |
Re: what is bind() method in javascript? Bind creates a new function that will have this set to the first parameter passed to bind(). Here's an example that shows how to use bind to pass a member method around that has the correct this: 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(); // not bound, 'this' is not myButton var boundClick = myButton.click.bind(myButton); boundClick(); // bound, 'this' is myButton Which prints out: OK clicked undefined clicked OK clicked You can also add extra parameters after the 1st parameter and bind will pass in those values to the original function before passing in the extra parameters you pass to the bound function: // Example showing binding some parameters var sum = function(a, b) { return a + b; }; var add5 = sum.bind(null, 5); console.log(add5(10)); Which prints out: |
| All times are GMT -6. The time now is 12:08 AM. |