@@ -13,30 +13,32 @@ example.
13
13
In the functions section, we saw how functions have access to changing
14
14
variable values. The same sort of behavior exists with functions defined within
15
15
loops -- the function "sees" the change in the variable's value even after the
16
- function is defined, resulting in all clicks alerting 5.
16
+ function is defined, resulting in each function referencing the last value stored
17
+ in the variable.
17
18
18
19
<div class =" example " markdown =" 1 " >
19
- How to lock in the value of i?
20
+ Each function executed within the loop will reference the last value stored in i (5)
20
21
21
22
/* this won't behave as we want it to; */
22
- /* every click will alert 5 */
23
+ /* every 100 milliseconds, 5 will alert */
23
24
for (var i=0; i<5; i++) {
24
- $('<p>click me</p>').appendTo('body').click (function() {
25
- alert(i);
26
- } );
25
+ setTimeout (function() {
26
+ alert(i);
27
+ }, i*100 );
27
28
}
28
29
</div >
29
30
30
- < div class = " example " markdown = " 1 " >
31
- Locking in the value of i with a closure
31
+ Closures can be used to prevent this by creating a unique scope for
32
+ each iteration -- storing each unique value of the variable within it's scope.
32
33
34
+ <div class =" example " markdown =" 1 " >
33
35
/* fix: “close” the value of i inside createFunction, so it won't change */
34
36
var createFunction = function(i) {
35
37
return function() { alert(i); };
36
38
};
37
39
38
40
for (var i=0; i<5; i++) {
39
- $('<p>click me</p>').appendTo('body').click( createFunction(i));
41
+ setTimeout( createFunction(i), i*100 );
40
42
}
41
43
</div >
42
44
@@ -85,22 +87,22 @@ implementation only, may be sufficient as a temporary bridge until `bind` is wid
85
87
adopted according to the specification.
86
88
87
89
<div class =" example " markdown =" 1 " >
88
- if ( !Function.prototype.bind ) {
89
- Function.prototype.bind = function( obj ) {
90
- var slice = [ ] .slice,
91
- args = slice.call(arguments, 1),
92
- self = this,
93
- nop = function () {},
94
- bound = function () {
95
- return self.apply( this instanceof nop ? this : ( obj || {} ),
96
- args.concat( slice.call(arguments) ) );
97
- };
98
-
99
- nop.prototype = self.prototype;
100
- bound.prototype = new nop();
101
- return bound;
102
- };
103
- }
90
+ if ( !Function.prototype.bind ) {
91
+ Function.prototype.bind = function( obj ) {
92
+ var slice = [].slice,
93
+ args = slice.call(arguments, 1),
94
+ self = this,
95
+ nop = function () {},
96
+ bound = function () {
97
+ return self.apply( this instanceof nop ? this : ( obj || {} ),
98
+ args.concat( slice.call(arguments) ) );
99
+ };
100
+
101
+ nop.prototype = self.prototype;
102
+ bound.prototype = new nop();
103
+ return bound;
104
+ };
105
+ }
104
106
</div >
105
107
106
108
One of the simplest uses of ` bind ` is making a function, which regardless of how it's
@@ -110,30 +112,30 @@ expecting it to the use the origin object as it's `this`. This however can be so
110
112
by creating a bound function using the original object as demonstrated below.
111
113
112
114
<div class =" example " markdown =" 1 " >
113
- //lets manipulate "this" with a basic example
114
- var user = "johnsmith",
115
- module = {
116
- getUser: function(){
117
- return this.user;
118
- },
119
- user: "janedoe"
120
- };
121
-
122
- //module.getUser() is called where "module" is "this" and "module.user" is returned.
123
- module.getUser();
124
- //janedoe
125
-
126
- //let's now store a reference in the global version of "this"
127
- var getUser = module.getUser;
128
-
129
- //getUser() called, "this" is global, "user" is returned
130
- getUser();
131
- //johnsmith
132
-
133
- //store a ref with "module" bound as "this"
134
- var boundGetUser = getUser.bind(module);
135
-
136
- //boundGetUser() called, "module" is "this" again, "module.user" returned.
137
- boundGetUser();
138
- //janedoe
115
+ //lets manipulate "this" with a basic example
116
+ var user = "johnsmith",
117
+ module = {
118
+ getUser: function(){
119
+ return this.user;
120
+ },
121
+ user: "janedoe"
122
+ };
123
+
124
+ //module.getUser() is called where "module" is "this" and "module.user" is returned.
125
+ module.getUser();
126
+ //janedoe
127
+
128
+ //let's now store a reference in the global version of "this"
129
+ var getUser = module.getUser;
130
+
131
+ //getUser() called, "this" is global, "user" is returned
132
+ getUser();
133
+ //johnsmith
134
+
135
+ //store a ref with "module" bound as "this"
136
+ var boundGetUser = getUser.bind(module);
137
+
138
+ //boundGetUser() called, "module" is "this" again, "module.user" returned.
139
+ boundGetUser();
140
+ //janedoe
139
141
</div >
0 commit comments