Skip to content

Commit a2b6705

Browse files
committed
edited function patterns
1 parent a2b3e25 commit a2b6705

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

function-patterns/callback.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
// find the nodes and hide them as you go
4040
findNodes(hide);
41+
42+
4143
</script>
4244
</body>
4345
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
var conf = {
10+
username: "shichuan",
11+
first: "Chuan",
12+
last: "Shi"
13+
};
14+
addPerson(conf);
15+
</script>
16+
</body>
17+
</html>

function-patterns/currying.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,63 @@
66
</head>
77
<body>
88
<script>
9+
/***
10+
function application
11+
***/
12+
// define a function
13+
var sayHi = function (who) {
14+
return "Hello" + (who ? ", " + who : "") + "!";
15+
};
16+
17+
// invoke a function
18+
sayHi(); // "Hello"
19+
sayHi('world'); // "Hello, world!"
20+
21+
// apply a function
22+
sayHi.apply(null, ["hello"]); // "Hello, hello!"
23+
24+
var alien = {
25+
sayHi: function (who) {
26+
return "Hello" + (who ? ", " + who : "") + "!";
27+
}
28+
};
29+
30+
alien.sayHi('world'); // "Hello, world!"
31+
sayHi.apply(alien, ["humans"]); // "Hello, humans!"
32+
33+
// the second is more efficient, saves an array
34+
sayHi.apply(alien, ["humans"]); // "Hello, humans!"
35+
sayHi.call(alien, "humans"); // "Hello, humans!"
36+
37+
/***
38+
partial application
39+
***/
40+
41+
// for illustration purposes
42+
// not valid JavaScript
43+
44+
// we have this function
45+
function add(x, y) {
46+
return x + y;
47+
}
48+
49+
// and we know the arguments
50+
add(5, 4);
51+
52+
// step 1 -- substitute one argument
53+
function add(5, y) {
54+
return 5 + y;
55+
}
56+
57+
// step 2 -- substitute the other argument
58+
function add(5, 4) {
59+
return 5 + 4;
60+
}
61+
62+
/***
63+
currying
64+
***/
65+
966
// a curried add()
1067
// accepts partial list of arguments
1168
function add(x, y) {

0 commit comments

Comments
 (0)