_* This rule is not the best practice but the one that we (developers at ownego) think is most suited for better software development.
Airbnb style guide: see here
Highly recommend reading full airbnb guide. Below is just some common rules that are extracted from that guide.
1- Do not use new
keyword. Use literal syntax instead.
// Not good
var newObj = new Object();
var newArr = new Array();
// Good
var newObj = {};
var newArr = [];
2- Copy Array
or Object
.
Be careful when working with Array
or Object
. If you use normal assignment or shallow copy, you only work on a reference to original value.
-
Array:
- Shallow copy: use javascript function slice(). See here. (
jQuery.extend()
is ok too)
var srcArr = ['abc', 'def', 'ghi']; var destArr = srcArr.slice(); console.log(destArr); // output // ['abc', 'def', 'ghi']
- Deep copy: use
jQuery.extend()
. See example below.
- Shallow copy: use javascript function slice(). See here. (
-
Object: For both shallow and deep copy use
jQuery.extend()
or write your own function (not recommend). More detailsvar srcObj = { apple: 0, banana: { price: 50, weight: 10 }, cherry: 100 }; var destObj = $.extend({}, srcObj); console.log(destObj); // output // Object destObj // { // apple: 0, // banana: { // price: 50, // weight: 10 // }, // cherry: 100 // }
3- Use Array.push()
.
var arr = [];
// Not good
arr[arr.length] = 'someValue';
// Good
arr.push('someValue');
4- Use one var
for each variable.
Advantage:
- Easier when add/remove a variable
- No need to worry about
,
or;
- Easy to convert to es6
Also avoid unassigned variable declaration.
5- Comparison and Equality See here
6- Conditional statements format
// if/else
if (condition) {
// do something
} else if (another condition) {
// do something
} ... {
// some stuff
} else {
// do something
}
// switch
switch (srcTarget) {
case value1:
// do something
break;
...
default:
// do something
}