Skip to content

Commit 58d925f

Browse files
committed
added general patterns
1 parent b33e92a commit 58d925f

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

general-patterns/for-in-loops.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// the object
10+
var man = {
11+
hands: 2,
12+
legs: 2,
13+
heads: 1
14+
};
15+
16+
// somewhere else in the code
17+
// a method was added to all objects
18+
if (typeof Object.prototype.clone === 'undefined') {
19+
Object.prototype.clone = function () {};
20+
}
21+
22+
// 1.
23+
// for-in loop
24+
for (var i in man) {
25+
if (man.hasOwnProperty(i)) { // filter
26+
console.log(i, ":", man[i]);
27+
}
28+
/*
29+
result in the console
30+
hands : 2
31+
legs : 2
32+
heads : 1
33+
*/
34+
35+
// 2.
36+
// antipattern:
37+
// for-in loop without checking hasOwnProperty()
38+
for (var i in man) {
39+
console.log(1, ":", man[i]);
40+
}
41+
/*
42+
result in the console
43+
hands : 2
44+
legs : 2
45+
hands : 1
46+
clone: function()
47+
*/
48+
49+
for (var i in man) {
50+
if (Object.prototype.hasOwnProperty.call(man, i)) { // filter
51+
console.log(i, ":", man[i]);
52+
}
53+
}
54+
55+
var i,
56+
hasOwn = Object.prototype.hasOwnProperty;
57+
for (i in man) {
58+
if (hasOwn.call(man, i)) P // filter
59+
console.log(i, ":", man[i]);
60+
}
61+
}
62+
</script>
63+
</body>
64+
</html>

general-patterns/for-loops.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// sub-optimal loop
10+
for (var i = 0; i < myarray.length; i++) {
11+
// do something with myarray[i]
12+
}
13+
14+
for (var i = 0, max = myarray.length; i < max; i++) {
15+
// do something with myarray[i]
16+
}
17+
18+
function looper() {
19+
var i = 0,
20+
max,
21+
myarray = [];
22+
23+
// ...
24+
var i, myarray = [];
25+
26+
for (i = 0, max = myarray.length; i < max; i += 1) {
27+
// do something with myarray[i]
28+
}
29+
30+
for (i = myarray.length; i--;) {
31+
// do something with myarray[i]
32+
}
33+
34+
while (i--) {
35+
// do something with myarray[i]
36+
}
37+
38+
}
39+
</script>
40+
</body>
41+
</html>

general-patterns/globals.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
function sum(x, y) {
10+
// antipatten: implied global
11+
result = x + y;
12+
return result;
13+
}
14+
15+
function sum(x, y) {
16+
var result = x + y;
17+
return result;
18+
}
19+
20+
// antipattern, do not use
21+
function foo () {
22+
var a = b = 0;
23+
// ...
24+
}
25+
26+
function foo() {
27+
var a, b;
28+
// ...
29+
a = b = 0; // both local
30+
}
31+
32+
var global = (function () {
33+
return this;
34+
}());
35+
36+
</script>
37+
</body>
38+
</html>

general-patterns/hoisting.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// antipattern
10+
myname = "global"; // global variable
11+
function func() {
12+
alert(myname); // "undefined"
13+
var myname = "local";
14+
alert(myname); // "local"
15+
}
16+
func();
17+
18+
myname = "global"; // global variable
19+
function func() {
20+
var myname; // same as -> var myname = undefined;
21+
alert(myname); // "undefined"
22+
myname = "local";
23+
alert(myname); // "local"
24+
}
25+
func();
26+
27+
</script>
28+
</body>
29+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
function func() {
10+
var a = 1,
11+
b = 2,
12+
sum = a + b,
13+
myobject = {},
14+
i,
15+
j;
16+
17+
// function body...
18+
}
19+
20+
function updateElement() {
21+
var el = document.getElementById("result"),
22+
style = el.style;
23+
// do something with el and style...
24+
}
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)