You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: wordpress-coding-standards/javascript.md
+19-18Lines changed: 19 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ These rules encourage liberal spacing for improved developer readability. The mi
49
49
50
50
Whitespace can easily accumulate at the end of a line – avoid this, as trailing whitespace is caught as an error in JSHint. One way to catch whitespace buildup is enabling visible whitespace characters within your text editor.
51
51
52
-
### Objects
52
+
### Object Declarations
53
53
54
54
Object declarations can be made on a single line if they are short (remember the line length guidelines). When an object declaration is too long to fit on one line, there must be one property per line. Property names only need to be quoted if they are reserved words or contain special characters:
55
55
@@ -67,11 +67,11 @@ var arr = [
67
67
4,
68
68
15,
69
69
];
70
-
70
+
71
71
// Acceptable for small objects and arrays
72
72
var obj = { ready:9, when:4, 'you are':15 };
73
73
var arr = [ 9, 4, 15 ];
74
-
74
+
75
75
// Bad
76
76
var obj = { ready:9,
77
77
when:4, 'you are':15 };
@@ -85,17 +85,17 @@ Always include extra spaces around elements and arguments:
85
85
86
86
```javascript
87
87
array = [ a, b ];
88
-
88
+
89
89
foo( arg );
90
-
90
+
91
91
foo( 'string', object );
92
-
92
+
93
93
foo( options, object[ property ] );
94
-
94
+
95
95
foo( node, 'property', 2 );
96
-
96
+
97
97
prop = object[ 'default' ];
98
-
98
+
99
99
firstArrayElement = arr[ 0 ];
100
100
```
101
101
@@ -300,10 +300,10 @@ If an abbreviation or an acronym occurs at the start of a variable name, it must
300
300
```javascript
301
301
// "Id" is an abbreviation of "Identifier":
302
302
constuserId=1;
303
-
303
+
304
304
// "DOM" is an acronym of "Document Object Model":
305
305
constcurrentDOMDocument=window.document;
306
-
306
+
307
307
// Acronyms and abbreviations at the start of a variable name are consistent
308
308
// with camelcase rules covering the first letter of a variable or class.
309
309
constdomDocument=window.document;
@@ -327,7 +327,7 @@ class Earth {
327
327
returnEarth.humans;
328
328
}
329
329
}
330
-
330
+
331
331
Earth.humans= [];
332
332
```
333
333
@@ -345,10 +345,10 @@ Comments come before the code to which they refer, and should always be preceded
345
345
346
346
```javascript
347
347
someStatement();
348
-
348
+
349
349
// Explanation of something complex on the next line
350
350
$( 'p' ).doSomething();
351
-
351
+
352
352
// This is a comment that is long enough to warrant being stretched
353
353
// over the span of multiple lines.
354
354
```
@@ -381,10 +381,11 @@ These are the preferred ways of checking the type of an object:
381
381
- Element: `object.nodeType` or `_.isElement( object )`
382
382
- null: `object === null`
383
383
- null or undefined: `object == null`
384
-
- undefined: <ul>- Global Variables: `typeof variable === 'undefined'`
385
-
- Local Variables: `variable === undefined`
386
-
- Properties: `object.prop === undefined`
387
-
- Any of the above: `_.isUndefined( object )` </li></ul>
384
+
- undefined:
385
+
- Global Variables: `typeof variable === 'undefined'`
386
+
- Local Variables: `variable === undefined`
387
+
- Properties: `object.prop === undefined`
388
+
- Any of the above: `_.isUndefined( object )`
388
389
389
390
Anywhere Backbone or Underscore are already used, you are encouraged to use [Underscore.js](http://underscorejs.org/#isElement)'s type checking methods over jQuery's.
0 commit comments