forked from chuanxshi/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-declarations.html
More file actions
41 lines (35 loc) · 999 Bytes
/
function-declarations.html
File metadata and controls
41 lines (35 loc) · 999 Bytes
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
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Function Declarations
* Description: creating anonymous functions and assigning them to a variable
*/
// antipattern
function getData() {
}
// preferred
/* Benefits:
* 1. Makes it easier to understand "functions as an object".
* 2. It enforces good semicolon habits.
* 3. Doesn't have much of the baggage traditionally associated with functions and scope.
*/
var getData = function () {
};
// named function expression
/* Benefits:
* 1. Get's rid of (anonymous functions) when inspecting and debugging
* 2. Doesn't break IE with the added 'Fn' (function name is not the same as the identifier)
*/
var getData = function getDataFn () {
};
// References
// http://ejohn.org/blog/javascript-as-a-first-language/
// http://kangax.github.com/nfe/
</script>
</body>
</html>