- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjavascript.js
More file actions
Latest commit
231 lines (186 loc) · 6.01 KB
/
Copy pathjavascript.js
File metadata and controls
231 lines (186 loc) · 6.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
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
/* 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
//exception for a string containing a single quote "'" is allowed
//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
varMAGIC_NUMBER=3;
//if you need to create a constructor function, use a named function and start its name with a capital letter
functionMouse(length){
this.wiskers=length;
}
varaMouse=newMouse(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(vari=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
varout=functionO(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
returnO(number-1);
}
};
//advanced stuff
//functions can have private state
varhasPrivate=(function(){
varnum=0;
returnfunction(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
vartype='a';
vartypeName={t: 'track',a: 'artist'}[type];
//asynchronous calls should use deferreds
//the deferred returned from asyncCall will be resolved with an error occasionally
varasyncCall=function(param){
vardeferred=Deferred();
//pass functions, not strings to setTimeout
setTimeout(function(){
if(Math.random()<0.9){
deferred.resolve({message: param});
}
else{
deferred.resolve({error: 'whoops'});
}
},2000);
returndeferred.promise();
};
//use pipe to change the result or the state of a deferred when it is resolved
varwaitFor=asyncCall('something').pipe(function(result){
if(result.error){
//change the state (so fail is called below)
returnDeferred.reject(result.error);
}
//change the result
returnresult.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(){
}
);
//don't use setInterval - use setTimeout with a named function and reschedule if needed
setTimeout(functioncheck(){
if(isReady()){
doSomething();
}
else{
setTimeout(check,200);
}
},200);
//if order doesn't matter loop backwards (slightly quicker and fewer characters)
vararr=['a','b'];
varj=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
varobj={
prop: 'one',
other: 'two'
};
varnewArrFromObj=_.map(obj,function(value,key){
returnkey+' '+value;
});
varnewArrFromArr=_.map(arr,function(element,index){
return'['+index+']='+element;
});
//ternary
varq=bool ? 'yey' : 'nay';
//coercion / casting
//string
varstr=''+numValue;
//number
varnum=+str;
//boolean
varbool=!!num;
//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=(typeofoptions==='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
vartopics={};
vargetTopic=function(name){
returntopics[name]||(topics[name]={topicName: name});
};
})();
//blank line at end of file (aides concatenation of files)