Forced un-encapsulation
const { resolve } = require( 'path' );
resolve( './path/' );
Object construction & check for lengthy OR statements
const fruit = 'apple';
if( fruit === 'apple' || fruit === 'banana' || fruit === 'orange' || fruit === 'pear' ) {
console.log( fruit );
}
Compact:
const fruit = 'apple';
if( { apple:1, banana:1, orange:1, pear:1 }[ fruit ] ) {
console.log( fruit );
}
Defaults
let x = y || 10;
i's and floating point issues
0.1 + 0.2 === 0.3 // is false & === 0.30000000000000004
9007199254740992 + 1 // is equal to 9007199254740992
9007199254740992 + 2 // is equal to 9007199254740994
Comma operator
let a = 0;
let b = ( a++, 99 );
console.log( a ); // a will be equal to 1
console.log( b ); // b is equal to 99
NaN Problems
let a = NaN;
if( a === a )
// Yes, equal
else
// No, not equal...NaN...
Check for things
Is it a number?
( +x === x )
Is it a string?
( ''+x === x )
Is it null?
( x )
Is it NaN?
( x === x )
Is it Power of 2?
!( n & ( n - 1 ) )
is it truthy & definitely equal to something?
( !!x )
Avoid using try-catch-finally inside a loop
let object = [ 'foo', 'bar' ], i;
for ( i = 0, len = object.length; i <len; i++ ) {
try {
// do something that throws an exception
}
catch ( e ) {
// handle exception
}
}
Use:
let object = [ 'foo', 'bar' ], i;
try {
for ( i = 0, len = object.length; i <len; i++ ) {
// do something that throws an exception
}
}
catch ( e ) {
// handle exception
}
Use let
and const
as frequently as possible
var a = 1;
function go() {
console.log( a );
var a = 2;
}
go();
// a = undefined
~
or something else?
Bitwise complement double operand
console.log( Math.floor( 55.55 ) ); // 55
console.log( ~~55.55 ); // 55
Bitwise complement single operand
let names = [ 'nick', 'matt' ];
if( ~names.indexOf( 'mike' ) )
//found
else
//not found
Arugment lists
function x( ...args ) {
console.log( args ); // [ 0, 1, 2, 3, 4 ] >> very nice
}
function y( args ) {
console.log( args ); // 0 >> why
console.log( arguments ); // { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4 } >> ugly
}
x( 0, 1, 2, 3, 4 );
y( 0, 1, 2, 3, 4 );
Key value auto-referencing
let body = 'words words words';
let obj = { body };
console.log( obj ); // { body: 'words words words' }
Promise passing an object
.then( x => {
return { key: 'value', new: x };
} );
.then( new => ( { key: 'value', new } ) );
Recursive Promise Resolving
let resolveAll = P => {
let map = ( pl, next ) => Promise.all( pl.map( p => Promise.resolve( p ).then( next ) ) ),
props = o => {
let pToR = [];
_.map( _.keys( o ), k => pToR.push( Promise.resolve( o[ k ] ).then( v => _.set( o, k, v ) ) ) );
return Promise.all( pToR ).return( o );
},
rNP = o => Promise.resolve( o ).then( o => {
if( _.isArray( o ) ) return map( o, rNP );
else if( _.isPlainObject( o ) ) {
let oa = {};
for( let ka in o ) oa[ ka ] = rNP( o[ ka ] );
return props( oa );
}
return o;
} );
return ( rNP )( P );
};
Compressing functions
let x = function( a ) {
console.log( a );
};
let y = b => {
console.log( b );
};
x( 'a' );
y( 'b' );
Variable passing
.then( x => console.log( x ) ) // logs x
.then( console.log ) // logs any incoming variable
This is fun
let f=(x,y,n=1)=>x?(console.log(x),f(x.slice(0,-1),y)):n++<y.length?(console.log(y.slice(0,n)),f(x,y,n)):''
f( 'Hello', 'World' );