forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturning-functions.html
More file actions
40 lines (37 loc) · 844 Bytes
/
returning-functions.html
File metadata and controls
40 lines (37 loc) · 844 Bytes
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
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Returning functions
Description: one function returns another function or create another function on-demand
*/
var setup = function () {
console.log(1);
return function () {
console.log(2);
};
};
// using the setup function
var my = setup(); // alerts 1
my(); // alerts 2
var setup = function (count) {
var count = 0;
return function () {
return (count += 1);
};
};
// usage
var next = setup();
//next(); // returns 1
//next(); // returns 2
//next(); // returns 3
// reference
// http://www.jspatterns.com/
// http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate
</script>
</body>
</html>