This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function letsWasteParam(array, getOriginal) { | |
for (var i = 0; i < array.length; i++) { | |
array[i] = array[i] * 2; | |
} | |
if (!getOriginal) { | |
return 'Doubled array: ' + array; | |
} else { | |
return 'Original array: ' + array; // oops | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES5 | |
function safeGuardES5(x) { | |
x = x || 7; | |
// magic | |
} | |
function safeGuardES5immutable(paramX) { | |
var x = paramX || 7; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doingSomething3D(params) { | |
var x = params.x || 0; | |
var y = params.y || 0; | |
var z = params.z || 0; | |
return 'x: ' + x +', y: ' + y + ', z: ' + z; | |
} | |
function theNewBlack({ x = 0, y = 0 , z = 0 } = {}) { | |
return `x: ${x}, y: ${y}, z: ${z}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ArrayContainer(array) { | |
this.array = array; | |
} | |
// before | |
ArrayContainer.prototype.getNewOrder = function (startAt) { | |
if (!startAt) { | |
return this.array; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const object1 = { | |
name: 'John', | |
surname: 'Carbonlover' | |
}; | |
const object2 = Object.assign(object1, { | |
name: 'Frank' | |
}); | |
// object1.name is now 'Frank', the same like object2’s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cantTouchThis = { | |
id: 1, | |
type: 'fromage', | |
items: { | |
knife: 'bowie', | |
fork: 'krautrock' | |
} | |
}; | |
Object.freeze(cantTouchThis); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacciProcessed(x) { | |
return new Promise(resolve => { | |
const calculation = function (resolveFn, n = 0, a = 0, b = 1) { | |
if (n > 1) { | |
setImmediate(() => { | |
calculation(resolveFn, n - 1, b, a + b); | |
}); | |
} | |
// else |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// piece taken from: https://medium.com/javascript-scene/reduce-composing-software-fe22f0c39a1d#.4chgx5fc0 | |
const map = (fn, arr) => arr.reduce((acc, item, index, arr) => { | |
return acc.concat(fn(item, index, arr)); | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// piece taken from: https://medium.com/javascript-scene/reduce-composing-software-fe22f0c39a1d#.4chgx5fc0 | |
const filter = (fn, arr) => arr.reduce((newArr, item) => { | |
return fn(item) ? newArr.concat([item]) : newArr; | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const slice = (array, from, to) => { | |
const startIndex = from < 0 ? array.length + from : from; | |
const endIndex = to ? (to < 0 ? index < array.length + to : index < to) : index < array.length; | |
return array.reduce((accumulated, current, index) => { | |
return index >= startIndex && index < endIndex ? accumulated.concat(current) : accumulated; | |
}, []); | |
}; |