Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b9a243

Browse files
committed
added comments to function patterns
1 parent 8a465a4 commit 9b9a243

10 files changed

+92
-57
lines changed

function-patterns/callback.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Callback patterns
10+
Description: when you pass function A to function B as a parameter, function A is a callback function
11+
*/
12+
913
var findNodes = function (callback) {
1014
var i = 100000,
1115
nodes = [],

function-patterns/configuration-objects.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Configuration objects
10+
Description: keep control of function arguments and makes it easily configurable
11+
*/
12+
913
var conf = {
1014
username: "shichuan",
1115
first: "Chuan",

function-patterns/currying.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Currying
10+
Description: used to create new functions dynamically by partially applying a set of arguments
11+
*/
12+
913
/***
1014
function application
1115
***/

function-patterns/immediate-functions.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Immediate functions
10+
Description: syntax that enables function execution as soon as it is defined
11+
*/
12+
913
(function () {
1014
console.log('watch out!');
1115
}());

function-patterns/immediate-object-initialization.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Immediate object initialization
10+
Description: this pattern is mainly suitable for one-off tasks
11+
*/
12+
913
({
1014
// here you can define setting values
1115
// a.k.a. configuration constants

function-patterns/init-time-branching.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Init-time branching
10+
Description: branch code once during initlization initialization
11+
*/
12+
913
// BEFORE
1014
/* var utils = {
1115
addListener: function (el, type, fn) {

function-patterns/memoization.html

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,75 @@
66
</head>
77
<body>
88
<script>
9-
// antipattern
10-
// reason see: https://github.com/shichuan/javascript-patterns/issues/6
11-
var myFunc = function (param) {
12-
if (!myFunc.cache[param]) {
13-
var result = {};
14-
// ... expsensive operation ...
15-
myFunc.cache[param] = result;
16-
}
17-
return myFunc.cache[param];
18-
};
9+
/* Title: Memoization
10+
Description: use function properties to avoid repeated computation
11+
*/
12+
13+
// antipattern
14+
// reason see: https://github.com/shichuan/javascript-patterns/issues/6
15+
var myFunc = function (param) {
16+
if (!myFunc.cache[param]) {
17+
var result = {};
18+
// ... expsensive operation ...
19+
myFunc.cache[param] = result;
20+
}
21+
return myFunc.cache[param];
22+
};
1923

20-
// cache storage
21-
myFunc.cache = {};
24+
// cache storage
25+
myFunc.cache = {};
2226

2327

24-
// preferred
28+
// preferred method 1
29+
// only one argument using param
30+
31+
var myFunc = function (param) {
32+
if (!myFunc.cache.hasOwnProperty(param)) {
33+
var result = {};
34+
// ... expsensive operation ...
35+
myFunc.cache[param] = result;
36+
}
37+
return myFunc.cache[param];
38+
};
2539

26-
/*
27-
only one argument using param
28-
*/
29-
var myFunc = function (param) {
30-
if (!myFunc.cache.hasOwnProperty(param)) {
31-
var result = {};
32-
// ... expsensive operation ...
33-
myFunc.cache[param] = result;
34-
}
35-
return myFunc.cache[param];
36-
};
40+
// cache storage
41+
myFunc.cache = {};
3742

38-
// cache storage
39-
myFunc.cache = {};
4043

44+
// preferred method 2
45+
// multiple arguments using JSON stringify
46+
47+
var myFunc = function () {
48+
var cachekey = JSON.stringify(Array.prototype.slice.call(arguments)),
49+
result;
50+
if (!myFunc.cache[cachekey]) {
51+
result = {};
52+
// ... expensive operation ...
53+
myFunc.cache[cachekey] = result;
54+
}
55+
return myFunc.cache[cachekey];
56+
};
4157

42-
/*
43-
multiple arguments using JSON stringify
44-
*/
45-
var myFunc = function () {
46-
var cachekey = JSON.stringify(Array.prototype.slice.call(arguments)),
47-
result;
48-
if (!myFunc.cache[cachekey]) {
49-
result = {};
50-
// ... expensive operation ...
51-
myFunc.cache[cachekey] = result;
52-
}
53-
return myFunc.cache[cachekey];
54-
};
58+
// cache storage
59+
myFunc.cache = {};
5560

56-
// cache storage
57-
myFunc.cache = {};
5861

62+
// preferred method 3
63+
// multiple arguments using arguments.callee
64+
65+
var myFunc = function (param) {
66+
var f = arguments.callee,
67+
result;
68+
if (!f.cache[param]) {
69+
result = {};
70+
// ... expensive operation ...
71+
f.cache[param] = result;
72+
}
73+
return f.cache[param];
74+
};
5975

60-
/*
61-
multiple arguments using arguments.callee
62-
*/
63-
var myFunc = function (param) {
64-
var f = arguments.callee,
65-
result;
66-
if (!f.cache[param]) {
67-
result = {};
68-
// ... expensive operation ...
69-
f.cache[param] = result;
70-
}
71-
return f.cache[param];
72-
};
73-
74-
// cache storage
75-
myFunc.cache = {};
76+
// cache storage
77+
myFunc.cache = {};
7678
</script>
7779
</body>
7880
</html>

function-patterns/returning-functions.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Returning functions
10+
Description: one function returns another function or create another function on-demand
11+
*/
12+
913
var setup = function () {
1014
console.log(1);
1115
return function () {

function-patterns/self-defining-functions.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
</head>
77
<body>
88
<script>
9+
/* Title: Self-defining functions
10+
Description: self-overwrite with new bodies to do less work from the second invocation and after
11+
*/
12+
913
var scareMe = function () {
1014
alert("Boo!");
1115
scareMe = function () {

function-patterns/self-overwrite.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</head>
77
<body>
88
<script>
9+
910
function next() {
1011
var count = 1;
1112
next = function() {

0 commit comments

Comments
 (0)