If you'd like to make a change to it, then propose it to the group and lets make the change
- The fundementals
- JavaScript data types and data structures
- Concurrency model and Event Loop
- The Node.js event Loop and timers
- Memory managment / Garbage collector
Read and memorize this.
Familiarize yourself with the Google JavaScript style rules. Once you get it, use the following style rules at Augur:
- Always use 4 indent spacing
- Use tabs, not spaces
- Always use a space (nbsp) between characters to make code easily digestable at a glance, for example:
let x = 'variable';
// always end declarations in semi-colons => ;
// always 'use strict' on top of scripts, it improves performance
'use strict';
let a = 1; // always use let when working with variables. let is bracket scoped
const SOME_CONST = true; // use const when declaring and reusing a value. const is bracket scoped
var c = 'string'; // use var when you cannot use let or const
function name( arg1 = 'default', arg2 = {}, arg3 = false, ...restOfTheArgs ) {
return [ arg1, arg2, arg3, restOfTheArgs ];
}
name( undefined, undefined, null, 1, 2, 3, 'abc' ); // returns [ 'default', {}, null, 1, 2, 3, 'abc' ]
function tellMeTheDifference( someArg ) {
this.property = 'correct context';
let arrowFunc = ()=> {
console.log({
details: 'by default, arrow functions inherit context',
argumentVector: arguments,
context: this
});
};
let normalFunc = function() {
console.log({
details: 'by default, normal functions do not inherit context',
argumentVector: arguments,
context: this
});
}
normalFunc();
arrowFunc();
}
new tellMeTheDifference('this is a default argument value');
const sum = ( a, b )=> a + b;
sum( 1, 9 ); // returns 10
Always use function. Only use arrow functions if you intend to make the function inherit context.
- Always use ===
- never use ==
let a = false;
let b = 0;
let c = [];
let d = {
key: 'value'
};
let e;
if ( a === true ) {
console.log( true );
} else if ( b === 1 ) {
console.log( 'also', true );
} else if ( e !== undefined || d.constructor === Object && d.key === undefined ) {
// do something
} else if ( c.constructor === Array ) {
console.log( d );
} else {
console.log('this never gets called');
}
let array = [ 1, 2, 3 ];
array[ array.length ] = 4;
//is better than
array.push(4);
/*
the for loop is fast and easy to read.
use it when ordered iteration matters.
*/
let array = [ 1, 2, 3 ];
for ( let i = 0, j = array.length; i < j; i++ ) {
// do something with array[i]
}
/*
This is the fastest loop in JavaScript, b/c it requires
the least amount of computation to continue iterating.
Use always. When not possible to use, then use the for loop.
*/
let array = [ 1, 2, 3 ];
let i = array.length;
while ( i-- !== 0 ) {
//do something with array[i]
}
let obj = {
a: 1,
b: 2,
c: 3
};
for ( let property in obj ) {
//do something with obj[ property ];
}
let set = new Set([ 1, 2, 3 ]);
/*
Get each element in the set
*/
for ( let element of set ) {
//do something with each element;
}
let map = new Map([ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]);
/*
Get each element in the set
*/
for ( let [ key, value ] of map ) {
//do something with each key, value
}
Best practice for optimal performance.
Always answer the question:
Am I creating new object at run-time? Or am I creating a pointer that's getting re-used?
This causes weird bugs if you screw it up.
let clone = Object.assign( {}, obj );
let clone = array.slice();
let primitive = 1;
let clone = primitive;
primitive = 0;
console.log( clone === 1, primitive === 0 ); // returns true, true
let string = '1'; // string.constructor is String
let number = +string; // number.constructor is Number
let oops = +'this is not a number!'; // oops now equals NaN, since this was a bad cast
Use Int + ''
let number = 1; // number.constructor is Number
let string = number + ''; // string.constructor is String
Remember, JSON.stringify randomly creates a string from an object. Do not expect the same string to be created through the serialization process. You'll need a plugin for that, such as json-stable-stringify on npm.
Use finite-state machines
const stateMachine = {
queue: [],
};