1"Variables has local and global scopes. Let's suppose that we have two variables
2with the same name. One is globally defined and the other is defined inside a
3function closure and we want to get the variable value which is inside the
4function closure. In that case we use this bind() method.
5
6code:
7
8var x = 9; // this refers to global "window" object here in the browser
9
10var person = {
11 x: 81, // local variable x = 81
12 getX: function() {
13 return this.x; // "this" = person , so this.x is the same as person.x
14 }
15};
16// the next line of code is the most important one (line 17)
17var y = person.getX; // It will return 9, because it will call global value of x(var x=9).
18
19var myFunc = y.bind(person); // It will return 81, because it will call local value of x, which is defined in the object called person(x=81).
20
21y(); // return 9 (the global variable);
22
23myFunc();" // return 81 (the loacl variable);
24
1bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function.
1<html>
2<input type="button" id="btnSubmit" name="btnName" class="btnclass" value="Click Me" />
3</html>
4
5$('input[name="btnName"]').click(function(){
6 //do stuff
7});