Skip to content

Instantly share code, notes, and snippets.

View jasonbellamy's full-sized avatar

Jason Bellamy jasonbellamy

View GitHub Profile
@jasonbellamy
jasonbellamy / es5-arguments-error.js
Created September 23, 2015 21:16
Arguments is an "array like" object so you cannot directly iterate over it.
var square = function () {
return arguments.map(function(arg) {
return arg * arg;
});
};
square(1, 2, 3, 4, 5); // => "TypeError: arguments.map is not a function
@jasonbellamy
jasonbellamy / es5-arguments.js
Last active September 23, 2015 21:20
We can convert arguments to a proper array with Array.prototype.slice and then we can iterate over it.
var square = function() {
var args = Array.prototype.slice.call(arguments); // convert arguments to array.
return args.map(function(arg) {
return arg * arg;
});
};
square(1, 2, 3, 4, 5); // => [1, 4, 9, 16, 25]
@jasonbellamy
jasonbellamy / es6-spread.js
Created September 23, 2015 21:22
Using the ..spread operator gives us a proper array from the start and lets us stop using the arguments object.
var square = function(...args) {
return args.map(function(arg) {
return arg * arg;
});
};
square(1, 2, 3, 4, 5); // => [1, 4, 9, 16, 25]
@jasonbellamy
jasonbellamy / es6-spread-bonus.js
Created September 23, 2015 21:25
Succinct version of the square function written in ES6
const square = (...args) => args.map((arg) => arg * arg);
square(1, 2, 3, 4, 5); // => [1, 4, 9, 16, 25]
@jasonbellamy
jasonbellamy / es5-arguments-imperative-sum-error.js
Created October 9, 2015 00:34
Imperative sum function that mistakenly tries to use the arguments object like an Array.
var sum = function () {
var result = 0;
arguments.forEach(function(number) {
result = result + number;
});
return result;
};
@jasonbellamy
jasonbellamy / es5-arguments-imperative-sum.js
Last active October 9, 2015 00:43
Imperative sum function that converts the arguments object to an Array.
var sum = function () {
var result = 0;
var args = Array.prototype.slice.call(arguments); // convert arguments into array
args.forEach(function(number) {
result = result + number;
});
return result;
};
@jasonbellamy
jasonbellamy / function-using-anonymous-function-wrappers.js
Created October 14, 2015 01:10
An example of a function thats uses unneeded anonymous function wrappers.
var stateManger = function () {
var active = false;
var setState = function(state) {
active = state;
};
return {
toggle: function () {
return setState(!active);
},
@jasonbellamy
jasonbellamy / function-using-bind.js
Last active October 19, 2015 16:02
An example of a function thats uses .bind instead of anonymous function wrappers.
var stateManger = function () {
var active = false;
var setState = function(state) {
active = state;
};
return {
toggle : setState.bind(this, !active),
setActive : setState.bind(this, true),
setInactive : setState.bind(this, false)
@jasonbellamy
jasonbellamy / reduce-map.doge
Last active January 12, 2016 22:59
Implementation of map & reduce in dogescript - https://dogescript.com/
shh reduce
such reduce much fn xs accum index
rly index === xs.length -1
wow accum
wow reduce(fn, xs, fn(accum, xs[index + 1]), index + 1)
shh map
such map much fn xs
such _map much previous current
function functionFactory({ type, ...rest }) {
switch(type) {
case 'hi':
return hello(rest);
case 'bye':
return goodbye(rest);
}
};
function hello({ name }) {