Skip to content

Commit f9869db

Browse files
committed
Merge pull request jquery#8 from gjohnson/master
Merging updates to replace jQuery examples in JavaScript 101 with library agnostic code/vanilla JS.
2 parents 25f4921 + 4c2d13a commit f9869db

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

content/javascript-101/closures.md

+53-51
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,32 @@ example.
1313
In the functions section, we saw how functions have access to changing
1414
variable values. The same sort of behavior exists with functions defined within
1515
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.
1718

1819
<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)
2021

2122
/* this won't behave as we want it to; */
22-
/* every click will alert 5 */
23+
/* every 100 milliseconds, 5 will alert */
2324
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);
2728
}
2829
</div>
2930

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.
3233

34+
<div class="example" markdown="1">
3335
/* fix: “close” the value of i inside createFunction, so it won't change */
3436
var createFunction = function(i) {
3537
return function() { alert(i); };
3638
};
3739

3840
for (var i=0; i<5; i++) {
39-
$('<p>click me</p>').appendTo('body').click(createFunction(i));
41+
setTimeout( createFunction(i), i*100 );
4042
}
4143
</div>
4244

@@ -85,22 +87,22 @@ implementation only, may be sufficient as a temporary bridge until `bind` is wid
8587
adopted according to the specification.
8688

8789
<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+
}
104106
</div>
105107

106108
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
110112
by creating a bound function using the original object as demonstrated below.
111113

112114
<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
139141
</div>

0 commit comments

Comments
 (0)