forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closures.html
66 lines (59 loc) · 2.86 KB
/
closures.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<title>Closures v1.0</title>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
</head>
<body>
<script>
function whichLocal(city) {
var cityPrefix = "In the city of ";
return function(area) {
var areaPrefix = " there is an area called ";
console.log ("this --->"+this+"<");
return cityPrefix + city + areaPrefix + area;
}
};
var local = whichLocal("Lisboa")("Alfama");
alert("whichLocal('Lisboa')('Alfama') --->"+local);
var placeInLisbon = whichLocal("Lisbon");
alert("Retention of local variables or arguments to a function ex: placeInLisbon('Alfama') = "+placeInLisbon("Alfama")+" -------------- placeInLisbon('Bairro Alto') = "+placeInLisbon("Bairro Alto"));
var placeInSintra = whichLocal("Sintra");
// local= place2("Portela");
alert("placeInSintra('Portela') = "+placeInSintra("Portela")+" -------------- placeInSintra('S.Pedro') = "+placeInSintra("S.Pedro"));
// Now we want to add the possibility to change city prefix - by shadowing
function whichGreatLocal(city) {
var cityPrefix = "In the city of ";
return function(area, cityPrefix) { //<-------------- we add a second parameter with the same name as a captured var
var areaPrefix = " there is an area called ";
console.log ("this --->"+this+"<");
return cityPrefix + city + areaPrefix + area;
}
};
var placeInLisbon = whichGreatLocal("Lisbon");
alert("Shadowing: whichGreatLocal('Alfama','Huge: ') = "+placeInLisbon("Alfama","Huge ")+" -------------- placeInLisbon('Bairro Alto','Great ') = "+placeInLisbon("Bairro Alto","Great "));
// Now the save as previous but we are shadowing the argument
function whichGreatLocal2(city) {
var cityPrefix = "In the city of ";
return function(area, city) { //<-------------- we add a second parameter with the same name as a captured argument
var areaPrefix = " there is an area called ";
console.log ("this --->"+this+"<");
return cityPrefix + city + areaPrefix + area;
}
};
var placeInLisbon = whichGreatLocal2("Lisbon");
alert("Shadowing2: whichGreatLocal2('Alfama','Huge: ') = "+placeInLisbon("Alfama","Huge ")+" -------------- placeInLisbon('Bairro Alto','Great ') = "+placeInLisbon("Bairro Alto","Great "));
// ------------ abstraction using closures -
function plucker(FIELD) {
return function(obj) {
return obj && obj[FIELD];
};
};
var getTitle = plucker("title");
alert("getTitle({name:'Nico',title:'engineer'}) --->"+getTitle({name:'Nico',title:'engineer'}));
alert("getTitle({name:'Nico',xtitle:'engineer'}) --->"+getTitle({name:'Nico',xtitle:'engineer'}));
alert("getTitle(27) --->"+getTitle(27));
console.log(document.title+"...... END..");
</script>
</body>
</html>