Permalink
Switch branches/tags
Nothing to show
Find file Copy path
e61e25b Jan 2, 2014
2 contributors

Users who have contributed to this file

@shichuan @sderickson
51 lines (41 sloc) 1.01 KB
<!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
*/
// antipattern 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>