forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.html
More file actions
50 lines (41 loc) · 1.01 KB
/
globals.html
File metadata and controls
50 lines (41 loc) · 1.01 KB
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
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: The Problem with Globals
* Description: The problem is that they are shared among all the code in your JavaScript application or web page
*/
// antipatten 1
function sum(x, y) {
// implied global
result = x + y;
return result;
}
// preferred 1
function sum(x, y) {
// a variable declared inside of a function is local to that function and not available outside the function
var result = x + y;
return result;
}
// antipattern 2
function foo() {
var a = b = 0;
// ...
}
// the preceding code snippet will behave as if you've typed the following
var a = (b = 0);
// preferred 2
function foo() {
var a, b;
// ...
a = b = 0; // both local
}
// References
// http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
</script>
</body>
</html>