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-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 / 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 / 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 / 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 / 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 / nfe.js
Created May 21, 2015 15:12
Named function expressions
/**
* When working with Named function expressions which one of the following is preffered and why?
*/
/* Use the same name for the function and the variable its assigned to.*/
var foo = function foo() {
return foo;
}
/* Use a different name for the function and the variable its assigned to. */
@jasonbellamy
jasonbellamy / index.html
Last active February 15, 2025 00:34
Modal overlay created using CSS box-shadow.
<div class="modal">
modal content
</div>
@jasonbellamy
jasonbellamy / index.html
Last active August 29, 2015 14:16
Modal overlay created using CSS fixed positioning.
<div class="modal">
<div class="modal-content">
modal content
</div>
<div class="modal-overlay"></div>
</div>
@jasonbellamy
jasonbellamy / modal-with-outline-overlay.css
Created February 26, 2015 22:52
Modal overlay created using CSS outline property.
.modal {
background: black;
color: rgb(255, 255, 255);
height: 200px;
left: 50%;
position: relative;
text-align: center;
top: 50%;
transform: translate(-50%, 50%);
width: 200px;
@jasonbellamy
jasonbellamy / variadic.js
Created December 2, 2014 16:07
Basic implmentation of a variadic helper function/
var variadic = function( fn ) {
var _slice = Array.prototype.slice;
var _length = fn.length;
if ( _length < 1 ) {
return fn;
}
if( _length === 1 ) {
return function () {