Skip to content

Commit 9a79758

Browse files
committed
Update JavaScript Name and Coding Conventions.md
some styles fix
1 parent 43c1a49 commit 9a79758

File tree

1 file changed

+85
-67
lines changed

1 file changed

+85
-67
lines changed

JavaScript Name and Coding Conventions.md

+85-67
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| Field name | CamelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
1010

1111

12-
Coding conventions are style guidelines for programming. They typically cover:
12+
##Coding conventions are style guidelines for programming. They typically cover:
1313

1414
1. Naming and declaration rules for variables and functions.
1515
1. Rules for the use of white space, indentation, and comments.
@@ -18,41 +18,42 @@ Coding conventions secure quality:
1818

1919
1. Improves code readability
2020
1. Make code maintenance easier
21-
***
21+
2222
Always use the same naming convention for all your code. For example:
23-
***
2423
1. Do use camelCasing for variable and function arguments names;
2524
2. Do use PascalCasing for function names and global variable;
2625
3. Constants (like PI) written in UPPERCASE;
2726
4. Do not use under_scores in variable, constants, function arguments or function names;
2827
5. Do not use hyphens in JavaScript names.
29-
***
3028

31-
## Naming Conventions
29+
30+
### Naming Conventions
3231

3332
Do use PascalCasing for function names:
3433

3534
```javascript
36-
function HelloWorld()
37-
{
38-
}
35+
function HelloWorld()
36+
{
37+
}
3938
```
4039

4140
Do use camelCasing for function arguments and local variables:
4241

43-
function Hello(isShow)
44-
{
45-
}
42+
```javascript
43+
function Hello(isShow)
44+
{
45+
}
4646

4747
firstName = "John";
48-
lastName = "Doe";
49-
50-
price = 19.90;
51-
discount = 0.10;
52-
53-
fullPrice = price * 100 / discount;
48+
lastName = "Doe";
49+
50+
price = 19.90;
51+
discount = 0.10;
52+
53+
fullPrice = price * 100 / discount;
54+
```
5455

55-
***Note: Don't start names with a $ sign. It will put you in conflict with many JavaScript library names.***
56+
*Note: Don't start names with a $ sign. It will put you in conflict with many JavaScript library names.*
5657

5758
#### Spaces Around Operators
5859

@@ -63,113 +64,130 @@ Examples:
6364
var x = y + z;
6465
var values = ["Volvo", "Saab", "Fiat"];
6566

66-
#### Code Indentation
67+
### Code Indentation
6768

6869
Always use 4 spaces for indentation of code blocks:
6970

7071
Functions:
7172

72-
function ToCelsius(fahrenheit)
73-
{
74-
return (5/9) * (fahrenheit-32);
75-
}
73+
```javascript
74+
function ToCelsius(fahrenheit)
75+
{
76+
return (5/9) * (fahrenheit-32);
77+
}
78+
```
7679

77-
***Note: Do not use tabs (tabulators) for indentation. Text editors interpret tabs differently.***
80+
*Note: Do not use tabs (tabulators) for indentation. Text editors interpret tabs differently.*
7881

79-
#### Statement Rules
82+
### Statement Rules
8083

8184
*General rules for simple statements: Always end simple statement with a semicolon.*
8285

8386
Examples:
84-
85-
var values = ["Volvo", "Saab", "Fiat"];
86-
87-
var person = {
88-
firstName: "John",
89-
lastName: "Doe",
90-
age: 50,
91-
eyeColor: "blue"
92-
};
93-
94-
#### General rules for complex (compound) statements:
95-
***
87+
88+
```javascript
89+
var values = ["Volvo", "Saab", "Fiat"];
90+
91+
var person = {
92+
firstName: "John",
93+
lastName: "Doe",
94+
age: 50,
95+
eyeColor: "blue"
96+
};
97+
```
98+
99+
### General rules for complex (compound) statements:
100+
96101
1. Put the opening bracket at the end of the first line.
97102
2. Use one space before the opening bracket.
98103
3. Put the closing bracket on a new line, without leading spaces.
99104
4. Do not end complex statement with a semicolon.
100-
***
101105

102106
Functions:
103107

104-
function toCelsius(fahrenheit) {
105-
return (5/9) * (fahrenheit-32);
106-
}
108+
```javascript
109+
function toCelsius(fahrenheit) {
110+
return (5/9) * (fahrenheit-32);
111+
}
112+
```
107113

108114
Loops:
109115

116+
```javascript
110117
for (i = 0; i < 5; i++) {
111118
x += i;
112119
}
113-
120+
```
114121

115122
Conditionals:
116123

117-
if (time < 20) {
118-
greeting = "Good day";
119-
} else {
120-
greeting = "Good evening";
121-
}
124+
```javascript
125+
if (time < 20) {
126+
greeting = "Good day";
127+
} else {
128+
greeting = "Good evening";
129+
}
130+
```
122131

123132

124-
#### Object Rules
133+
### Object Rules
125134

126135
General rules for object definitions:
127-
***
136+
128137
1. Place the opening bracket on the same line as the object name.
129138
2. Use colon plus one space between each property and it's value.
130139
3. Use quotes around string values, not around numeric values.
131140
4. Do not add a comma after the last property-value pair.
132141
5. Place the closing bracket, on a new line, without leading spaces.
133142
6. Always end an object definition with a semicolon.
134-
***
143+
135144
Example:
136145

137-
var person = {
138-
firstName: "John",
139-
lastName: "Doe",
140-
age: 50,
141-
eyeColor: "blue"
142-
};
146+
```javascript
147+
var person = {
148+
firstName: "John",
149+
lastName: "Doe",
150+
age: 50,
151+
eyeColor: "blue"
152+
};
153+
```
143154

144155
Short objects can be written compressed, on one line, like this:
145156

146-
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
157+
```javascript
158+
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
159+
```
147160

148-
***Line Length < 80***
161+
### Line Length < 80
149162

150163
For readability, avoid lines longer than 80 characters. If a JavaScript statement does not fit on one line, the best place to break it, is after an operator or a comma.
151164

152165
Example:
153166

154-
document.getElementById("demo").innerHTML = "Hello World.";
167+
```javascript
168+
document.getElementById("demo").innerHTML = "Hello World.";
169+
```
155170

156-
#### Loading JavaScript in HTML
171+
### Loading JavaScript in HTML
157172

158173
Use simple syntax for loading external scripts (the type attribute is not necessary):
159-
160-
<script src="myscript.js">
161174

162-
#### Accessing HTML Elements
175+
```html
176+
<script src="myscript.js">
177+
```
178+
179+
### Accessing HTML Elements
163180
164181
A consequence of using "untidy" HTML styles, might result in JavaScript errors. These two JavaScript statements will produce different results:
165182
166-
var obj = getElementById("Demo")
167-
168-
var obj = getElementById("demo")
183+
```javascript
184+
var obj = getElementById("Demo")
185+
var obj = getElementById("demo")
186+
```
169187
170188
*If possible, use it naming convention (as JavaScript) in HTML.*
171189
172-
#### File Extensions
190+
### File Extensions
173191
174192
1. HTML files should have a .html extension (not .htm);
175193
2. CSS files should have a .css extension;

0 commit comments

Comments
 (0)