forked from airbnb/javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyletest.js
More file actions
executable file
·952 lines (608 loc) · 16.3 KB
/
styletest.js
File metadata and controls
executable file
·952 lines (608 loc) · 16.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
// bad
var item1 = new Object();
// good
var item2 = {};
// Don't use [reserved words](http://es5.github.io/#x7.6.1) as keys. It won't work in IE8. [More info](https://github.com/airbnb/javascript/issues/61)
// bad
var superman1 = {
default: {clark: 'kent'},
private: true
};
// good
var superman2 = {
defaults: {clark: 'kent'},
hidden: true
};
// bad
var items1 = new Array();
// good
var items2 = [];
// If you don't know array length use Array#push.
var someStack = [];
// bad
someStack[someStack.length] = 'abracadabra';
// good
someStack.push('abracadabra');
// When you need to copy an array use Array#slice. [jsPerf](http://jsperf.com/converting-arguments-to-an-array/7)
var len1 = items.length;
var itemsCopy1 = [];
var i0;
// bad
for (i0 = 0; i0 < len; i0++) {
itemsCopy[i0] = items[i0];
}
// good
itemsCopy = items.slice();
// Use single quotes ' for strings
// bad
var name1 = "Bob Parr";
// good
var name2 = 'Bob Parr';
// bad
var fullName1 = "Bob " + this.lastName;
// good
var fullName2 = 'Bob ' + this.lastName;
// Strings longer than 80 characters should be written across multiple lines using string concatenation.
// Note: If overused, long strings with concatenation could impact performance. [jsPerf](http://jsperf.com/ya-string-concat) & [Discussion](https://github.com/airbnb/javascript/issues/40)
// bad
var errorMessage1 = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
// bad
var errorMessage2 = 'This is a super long error that \
was thrown because of Batman. \
When you stop to think about \
how Batman had anything to do \
with this, you would get nowhere \
fast.';
// good
var errorMessage3 = 'This is a super long error that ' +
'was thrown because of Batman. ' +
'When you stop to think about ' +
'how Batman had anything to do ' +
'with this, you would get nowhere ' +
'fast.';
// When programatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2).
var items3;
var messages;
var length2;
var i2;
messages = [{
state: 'success',
message: 'This one worked.'
}, {
state: 'success',
message: 'This one worked as well.'
}, {
state: 'error',
message: 'This one did not work.'
}];
length = messages.length;
// bad
function inbox1(messages) {
items = '<ul>';
for (i = 0; i < length; i++) {
items += '<li>' + messages[i].message + '</li>';
}
return items + '</ul>';
}
// good
function inbox2(messages) {
items = [];
for (i = 0; i < length; i++) {
items[i] = messages[i].message;
}
return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
}
// anonymous function expression, space between function and parantheses
// bad
var anonymous1 = function() {
return true;
};
// good
var anonymous2 = function () {
return true;
};
// named function expression, no space between function name and parantheses
// bad
var named1 = function named () {
return true;
};
// good
function named2() {
return true;
};
// immediately-invoked function expression (IIFE)
(function () {
console.log('Welcome to the Internet. Please follow me.');
})();
// Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears.
// Crockford: If a function literal is anonymous, there should be one space between the word function and the ( (left parenthesis). If the space is omited, then it can appear that the function's name is function, which is an incorrect reading.
// - **Note:** ECMA-262 defines a `block` as a list of statements. A function declaration is not a statement. [Read ECMA-262's note on this issue](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf#page=97).
// bad
if (currentUser) {
function test() {
console.log('Nope.');
}
}
// good
var test;
if (currentUser) {
test = function test() {
console.log('Yup.');
};
}
// Never name a parameter `arguments`, this will take precedence over the `arguments` object that is given to every function scope.
// bad
function nope(name, options, arguments) {
doStuff();
}
// good
function yup(name, options, args) {
doStuff();
}
// if possible Use dot notation when accessing properties.
var luke1 = {
jedi: true,
age: 28
};
// good - no problems with minimizers/obfuscators
var isJedi1 = luke1['jedi'];
// ok - take care with minimizers/obfuscators!
var isJedi2 = luke1.jedi;
// good
var prop = 'jedi';
var isJedi3 = like[prop];
// Use subscript notation `[]` when accessing properties with a variable.
var luke2 = {
jedi: true,
age: 28
};
function getProp(prop) {
return luke[prop];
}
var isJedi3 = getProp('jedi');
// Always use `var` to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that.
// bad
superPower1 = new SuperPower();
// good
var superPower2 = new SuperPower();
// Use one `var` declaration for each variable and declare each variable on a newline.
// bad
var items4 = getItems(),
goSportsTeam1 = true,
dragonball1 = 'z';
// better
var items5 = getItems();
var goSportsTeam2 = true;
var dragonball2 = 'z';
// Declare unassigned variables last. This is helpful when later on you might need to assign a variable depending on one of the previous assigned variables.
// bad
var i1, len1, dragonball1;
var items6 = getItems();
var goSportsTeam1 = true;
// bad
var i2;
var items7 = getItems();
var dragonball2;
var goSportsTeam2 = true;
var len2;
// good
var items8 = getItems();
var goSportsTeam3 = true;
var dragonball3;
var length;
var i3;
// Use `===` and `!==` over `==` and `!=`.
// Conditional expressions are evaluated using coercion with the `ToBoolean` method and always follow these simple rules:
// Use shortcuts.
// bad
if (name !== '') {
doStuff();
}
// good
if (name) {
doStuff();
}
// bad
if (collection.length > 0) {
doStuff();
}
// good
if (collection.length) {
doStuff();
}
// Use correct indentation
// bad
if (name !== '') {
doStuff();
}
else if (name === 'moo') {
doStuff();
}
else
{
doStuff();
}
// good
if (name) {
doStuff();
} else if (name === 'moo') {
doStuff();
} else {
doStuff();
}
// For more information see [Truth Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) by Angus Croll
// Use braces with all multi-line blocks - use multi-line blocks with all braces
function testFunction(test) {
// bad
if (test)
return false;
// good
if (test) return false;
// good
if (test) {
return false;
}
}
// bad
function example() { return false; }
// good
function example() {
return false;
}
// Use `/** ... */` for multiline comments. Include a description, specify types and values for all parameters and return values.
// bad
// make() returns a new element
// based on the passed in tag name
//
// @param <String> tag
// @return <Element> element
function make(tag) {
doStuff();
return element;
}
// good
/**
* make() returns a new element
* based on the passed in tag name
*
* @param <String> tag
* @return <Element> element
*/
function make(tag) {
doStuff();
return element;
}
// Use `//` for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment.
// bad
var active = true; // is current tab
// good
// is current tab
var active = true;
// bad
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
var type = this._type || 'no type';
return type;
}
// good
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
var type = this._type || 'no type';
return type;
}
// Use soft tabs set to 4 spaces
// bad
function testFunction1() {
var name;
}
// bad
function testFunction2() {
var name;
}
// good
function testFunction3() {
var name;
}
// Place 1 space before the leading brace.
// bad
function test(){
console.log('test');
}
// good
function test() {
console.log('test');
}
// bad
dog.set('attr',{
age: '1 year',
breed: 'Bernese Mountain Dog'
});
// good
dog.set('attr', {
age: '1 year',
breed: 'Bernese Mountain Dog'
});
// Set off operators with spaces.
// bad
var x=y+5;
// good
var x = y + 5;
// Use indentation when making long method chains.
// bad
$('#items').find('.selected').highlight().end().find('.open').updateCount();
// good
$('#items')
.find('.selected')
.highlight()
.end()
.find('.open')
.updateCount();
// bad
var leds = stage.selectAll('.led').data(data).enter().append('svg:svg').class('led', true)
.attr('width', (radius + margin) * 2).append('svg:g')
.attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.call(tron.led);
// good
var leds = stage.selectAll('.led')
.data(data)
.enter().append('svg:svg')
.class('led', true)
.attr('width', (radius + margin) * 2)
.append('svg:g')
.attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.call(tron.led);
// Leading commas: **Nope.**
// bad
var once1
, upon1
, aTime1;
// good
var once2,
upon2,
aTime2;
// bad
var hero1 = {
firstName: 'Bob'
, lastName: 'Parr'
, heroName: 'Mr. Incredible'
, superPower: 'strength'
};
// good
var hero2 = {
firstName: 'Bob',
lastName: 'Parr',
heroName: 'Mr. Incredible',
superPower: 'strength'
};
// Additional trailing comma: **Nope.** This can cause problems with IE6/7 and IE9 if it's in quirksmode. Also, in some implementations of ES3 would add length to an array if it had an additional trailing comma. This was clarified in ES5 ([source](http://es5.github.io/#D)):
// Edition 5 clarifies the fact that a trailing comma at the end of an ArrayInitialiser does not add to the length of the array. This is not a semantic change from Edition 3 but some implementations may have previously misinterpreted this.
// You may use trailing comma in server-side (Node.js) scripts
// bad (ok in nodejs)
var hero3 = {
firstName: 'Kevin',
lastName: 'Flynn',
};
var heroes1 = [
'Batman',
'Superman',
];
// good
var hero4 = {
firstName: 'Kevin',
lastName: 'Flynn'
};
var heroes2 = [
'Batman',
'Superman'
];
// use semicolons
// bad
(function () {
var name = 'Skywalker'
return name
})()
// good
(function () {
var name = 'Skywalker';
return name;
})();
// good
;(function () {
var name = 'Skywalker';
return name;
})();
// <a name='type-coercion'>Type Casting & Coercion</a>
// Perform type coercion at the beginning of the statement.
// Strings:
// => this.reviewScore = 9;
// bad
var totalScore = this.reviewScore + '';
// good
var totalScore = '' + this.reviewScore;
// bad
var totalScore = '' + this.reviewScore + ' total score';
// good
var totalScore = this.reviewScore + ' total score';
// Use `parseInt` for Numbers and always with a radix for type casting.
var inputValue = '4';
// bad
var val = new Number(inputValue);
// bad
var val = +inputValue;
// bad
var val = inputValue >> 0;
// bad
var val = parseInt(inputValue);
// good
var val = Number(inputValue);
// good
var val = parseInt(inputValue, 10);
// If for whatever reason you are doing something wild and `parseInt` is your bottleneck and need to use Bitshift for [performance reasons](http://jsperf.com/coercion-vs-casting/3), leave a comment explaining why and what you're doing.
// **Note:** Be careful when using bitshift operations. Numbers are represented as [64-bit values](http://es5.github.io/#x4.3.19), but Bitshift operations always return a 32-bit integer ([source](http://es5.github.io/#x11.7)). Bitshift can lead to unexpected behavior for integer values larger than 32 bits. [Discussion](https://github.com/airbnb/javascript/issues/109)
// good
/**
* parseInt was the reason my code was slow.
* Bitshifting the String to coerce it to a
* Number made it a lot faster.
*/
var val = inputValue >> 0;
// Booleans:
var age = 0;
// bad
var hasAge = new Boolean(age);
// good
var hasAge = Boolean(age);
// good
var hasAge = !!age;
// Naming Conventions
// Avoid single letter names. Be descriptive with your naming.
// bad
function q() {
doStuff();
}
// good
function query() {
// ..stuff..
}
// Use camelCase when naming objects, functions, and instances
// bad
var OBJEcttsssss = {};
var this_is_my_object = {};
function c() {};
var u = new user({
name: 'Bob Parr'
});
// good
var thisIsMyObject = {};
function thisIsMyFunction() {};
var user = new User({
name: 'Bob Parr'
});
// Use PascalCase when naming constructors or classes
// bad
function user(options) {
this.name = options.name;
}
var bad = new user({
name: 'nope'
});
// good
function User(options) {
this.name = options.name;
}
var good = new User({
name: 'yup'
});
// Name your functions. This is helpful for stack traces.
// ok
var log = function (msg) {
console.log(msg);
};
// better
var log = function log(msg) {
console.log(msg);
};
// Accessor functions for properties are not required
// If you do make accessor functions use getVal() and setVal('hello')
// bad
dragon.age();
// good
dragon.getAge();
// bad
dragon.age(25);
// good
dragon.setAge(25);
// If the property is a boolean, use isVal() or hasVal()
function testFunction() {
// bad
if (!dragon.age()) {
return false;
}
// good
if (!dragon.hasAge()) {
return false;
}
}
// It's okay to create get() and set() functions, but be consistent.
function Jedi(options) {
options || (options = {});
var lightsaber = options.lightsaber || 'blue';
this.set('lightsaber', lightsaber);
}
Jedi.prototype.set = function (key, val) {
this[key] = val;
};
Jedi.prototype.get = function (key) {
return this[key];
};
// Constructors
// Assign methods to the prototype object, instead of overwriting the prototype with a new object. Overwriting the prototype makes inheritance impossible: by resetting the prototype you'll overwrite the base!
function Jedi() {
console.log('new jedi');
}
// bad
Jedi.prototype = {
fight: function fight() {
console.log('fighting');
},
block: function block() {
console.log('blocking');
}
};
// good
Jedi.prototype.fight = function fight() {
console.log('fighting');
};
Jedi.prototype.block = function block() {
console.log('blocking');
};
// Methods can return `this` to help with method chaining.
// bad
Jedi.prototype.jump = function () {
this.jumping = true;
return true;
};
Jedi.prototype.setHeight = function (height) {
this.height = height;
};
var luke = new Jedi();
luke.jump(); // => true
luke.setHeight(20) // => undefined
// good
Jedi.prototype.jump = function () {
this.jumping = true;
return this;
};
Jedi.prototype.setHeight = function (height) {
this.height = height;
return this;
};
var luke = new Jedi();
luke.jump()
.setHeight(20);
// It's okay to write a custom toString() method, just make sure it works successfully and causes no side effects.
function Jedi(options) {
options || (options = {});
this.name = options.name || 'no name';
}
Jedi.prototype.getName = function getName() {
return this.name;
};
Jedi.prototype.toString = function toString() {
return 'Jedi - ' + this.getName();
};
// Events
// When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass a hash instead of a raw value. This allows a subsequent contributor to add more data to the event payload without finding and updating every handler for the event. For example, instead of:
// bad
$(this).trigger('listingUpdated', listing.id);
$(this).on('listingUpdated', function (e, listingId) {
// do something with listingId
});
// prefer:
// good
$(this).trigger('listingUpdated', {listingId: listing.id});
$(this).on('listingUpdated', function (e, data) {
// do something with data.listingId
});