forked from javascript-patterns/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
/* Title: Single var Pattern | ||
Description: use one var statement and declare multiple variables | ||
*/ | ||
<head> | ||
<title>JavaScript Patterns</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<script> | ||
/* Title: Single var Pattern | ||
Description: use one var statement and declare multiple variables | ||
*/ | ||
|
||
/* Benefits: | ||
* 1. Provides a single place to look for all the local variables needed by the function | ||
* 2. Prevents logical errors when a variable is used before it's defined | ||
* 3. Helps you remember to declare variables and therefore minimize globals | ||
* 4. Is less code (to type and to transfer over the wire) | ||
*/ | ||
/* Benefits: | ||
* 1. Provides a single place to look for all the local variables needed by the function | ||
* 2. Prevents logical errors when a variable is used before it's defined | ||
* 3. Helps you remember to declare variables and therefore minimize globals | ||
* 4. Is less code (to type and to transfer over the wire) | ||
*/ | ||
|
||
function func() { | ||
var a = 1 | ||
, b = 2 | ||
, sum = a + b | ||
, myobject = {} | ||
, i | ||
, j; | ||
|
||
// function body... | ||
} | ||
function func() { | ||
var a = 1 | ||
, b = 2 | ||
, sum = a + b | ||
, myobject = {} | ||
, i | ||
, j; | ||
|
||
function updateElement() { | ||
var el = document.getElementById("result") | ||
, style = el.style; | ||
// do something with el and style... | ||
} | ||
// function body... | ||
} | ||
|
||
// Preferred way | ||
// Move commas BEFORE vars | ||
// You'll not forget to add one when adding variable to the end of list | ||
function func() { | ||
var a = 1 | ||
function updateElement() { | ||
var el = document.getElementById("result") | ||
, style = el.style; | ||
// do something with el and style... | ||
} | ||
|
||
// Preferred way | ||
// Move commas BEFORE vars | ||
// You'll not forget to add one when adding variable to the end of list | ||
function func() { | ||
var a = 1 | ||
, b = 2 | ||
, sum = a + b | ||
, myobject = {} | ||
, i | ||
, j; | ||
|
||
// function body... | ||
} | ||
// function body... | ||
} | ||
|
||
// References | ||
// https://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/ | ||
</script> | ||
</body> | ||
</html> | ||
// References | ||
// https://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/ | ||
</script> | ||
</body> | ||
</html> |