forked from rmhodges/code-style
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
246 lines (197 loc) · 6.38 KB
/
javascript.js
File metadata and controls
246 lines (197 loc) · 6.38 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* The following Javascript file serves as the style guide for we7 */
//name your Javascript file with a camelCase name (.js), named after the primary object it returns, or primary function
//immediately executing function expression
//parenthesis around the function
//no space between function and ()
//space between () and {
(function() {
//code goes in here to avoid polluting the global scope
//always use semicolons
//indent 2 spaces
//no end of line comments
//comment before the thing you are commenting on
//single quote all strings, escaping where necessary
//declare vars where it makes sense to do so, combining into one var statement
//indent all variable names, on separate lines except for short unassigned at the end
//indent objects and functions
var
property = 'hello I\'m a string',
something = 'else',
//no space after [ or before ]
//space after comma
array = ['one', 'two'],
//no space before : but space after
object = {
foo: 'bar',
baz: function() {
return 'hiya!';
}
},
//lower camelCase for variable names
numValue = 42,
//start with a $ for jQuery objects
$document = $(document),
except, like, these, size, options;
//use upper case and underscores for constants
var MAGIC_NUMBER = 3;
//if you need to create a constructor function, use a named function and start its name with a capital letter
function Mouse(length) {
this.wiskers = length;
}
var aMouse = new Mouse(3);
//space after control statements and semicolons
//space around operators
//no space inside parenthesis (or [] or {})
//don't create functions inside loops
//see also loops below
for (var i = 0; i < array.length; i++) {
out(i + 2);
}
//open curly brackets on the same line
//close curly brackets on its own line
//always use curly brackets
var out = function O(number) {
//always use === for comparison
if (number === 1) {
return 'yes';
}
//except when comparing to null (matches undefined too)
if (number == null) {
return 'no';
}
//use isNaN() to check for NaN (Nan === NaN returns false)
if (isNaN(number)) {
return 'what?';
}
else {
//name your function only when it needs to call itself, and make the name short
return O(number - 1);
}
};
//advanced stuff
//functions can have private state
var hasPrivate = (function() {
var num = 0;
return function(name) {
num += 1;
return 'hello ' + name + '. I\'ve said hello ' + num + ' times';
};
})();
hasPrivate('Me');
// -> hello Me. I've said hello 1 times
hasPrivate('You');
// -> hello You. I've said hello 2 times
//avoid using 'this' inside functions, as it can change depending on how the function is called
//avoid creating and using constructors (a missed 'new' can cause havok)
//avoid switch statements or multiple ifs when you can (ab)use objects instead
var type = 'a';
var typeName = {t: 'track', a: 'artist'}[type];
//asynchronous calls should use deferreds
//the deferred returned from asyncCall will be resolved with an error occasionally
var asyncCall = function(param) {
var deferred = Deferred();
//pass functions, not strings to setTimeout
setTimeout(function() {
if (Math.random() < 0.9) {
deferred.resolve({message: param});
}
else {
deferred.resolve({error: 'whoops'});
}
}, 2000);
return deferred.promise();
};
//use pipe to change the result or the state of a deferred when it is resolved
var waitFor = asyncCall('something').pipe(function(result) {
if (result.error) {
//change the state (so fail is called below)
return Deferred.reject(result.error);
}
//change the result
return result.message.toUpperCase();
});
//multiple chained function calls start each on new line, indented
waitFor
.done(function(result) {
//result will be 'SOMETHING'
})
.fail(function(result) {
//will be called 1/10 times
//result will be 'whoops'
});
//passing a function to a function
withStuff(function() {
//anything
}, 'other', 'args');
//passing more than one function to a function - indent
withStuff(
function() {
},
'other', 'args', 'inline',
function() {
}
);
//watch out! array.splice() requires two arguments in IE
//this does nothing in IE, in other browsers it trims the array to the first element
array.splice(1);
//do this instead
array.splice(1, 1/0); //where 1/0 evaluates to Infinity
//don't use setInterval - use setTimeout with a named function and reschedule if needed
setTimeout(function check() {
if (isReady()) {
doSomething();
}
else {
setTimeout(check, 200);
}
}, 200);
//if order doesn't matter loop backwards (slightly quicker and fewer characters)
var arr = ['a','b'];
var j = arr.length;
while (j--) {
somethingWith(arr[i]);
}
//or you can be functional (works with objects too)
_.each(arr, doSomething);
//creating a modified array from an array or object - use map
var obj = {
prop: 'one',
other: 'two'
};
var newArrFromObj = _.map(obj, function(value, key) {
return key + ' ' + value;
});
var newArrFromArr = _.map(arr, function(element, index) {
return '[' + index + ']=' + element;
});
//ternary
var q = bool ? 'yey' : 'nay';
//coercion / casting
//string
var str = '' + numValue;
//number
var num = +str;
//boolean
var bool = !!num;
//shortcuts - ideally UglifyJS would do these automatically
//Infinity
var inf = 1/0;
//-Infinity
var ninf = 1/-0;
//NaN - although not shorter, should gzip better
var notanumber = 0/0;
//simple checks before doing something can be done with && (instead of if)
object && fire(object);
//setting a default value can be done with || (as long as null, undefined, '' or 0 are not supposed to be valid)
size = size || 100;
//defaults for objects (not a ternary because typeof null is 'object')
options = (typeof options === 'object') && object || {size: 100};
//or for getting a property - handles options or the property being unset
size = options && options.size || 100;
//one line assign if not set
var topics = {};
var getTopic = function(name) {
return topics[name] || (topics[name] = {topicName: name});
};
})();
//blank line at end of file (aides concatenation of files)