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
// a function returning `something` in javascript is a useful tool | |
function foo() { | |
return "foo"; | |
} | |
// create a reference | |
var bar; | |
// if we assign `bar` to `foo` | |
bar = foo; |
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
/* | |
Constructor vs Literal | |
*/ | |
// Array | |
var foo = new Array(); // constructor | |
var bar = []; // literal | |
// difference? | |
// constructor - inconsistent API |
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 lotto() { | |
function getBalls() { | |
var random = getRandom(1,59); | |
if(!balls.includes(random)) { | |
balls.push(random); | |
} | |
if(balls.length < 6) { | |
return getBalls(); | |
} | |
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
function stillContainsArray(arr) { | |
return arr.some(item => item instanceof Array); | |
} | |
function concat(arr) { | |
return arr.reduce((a,b) => a.concat(b), []); | |
} | |
function flatten(arr) { | |
while(stillContainsArray(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
var restaraunt = { | |
orders : [], | |
order : function(order) { | |
this.orders.push(order); | |
}, | |
total : function(orders) { | |
orders = orders || this.orders; | |
return orders.reduce(function(total, order) { | |
return total + order.cost; | |
}, 0); |
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
var PubSub = (function() { | |
let listeners = {}; | |
return { | |
listeners, | |
subscribe (evt, fn) { | |
if(!this.listeners.hasOwnProperty(evt)) { | |
this.listeners[evt] = []; | |
} | |
this.listeners[evt].push(fn); | |
return () => { |
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 capitalise = f => replace(/\b[a-z]/g, f => f.toUpperCase()); | |
const lowercaseilise = f => replace(/\b[A-Z]/g, f => f.toLowerCase()); |
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
var jQuery = (function() { | |
function jQuery(selector, context = document) { | |
return new jQuery.fn.init(selector, context); | |
} | |
jQuery.fn = jQuery.prototype ; | |
jQuery.fn.css = function(prop, value) { | |
this.pushStack.forEach(el => el.style[prop] = value); |
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
var selector = '#aoFooterSmallPrint'; | |
var addEvent = a => b => c => a.addEventListener(b, c); | |
var footerText = document.querySelector(selector); | |
var footerTextEvents = addEvent(footerText); | |
var footerTextOnClick = footerTextEvents('click'); | |
var footerTextOnMouseOver = footerTextEvents('mouseover'); | |
var log = evt => console.log(evt); | |
footerTextOnClick(log); | |
footerTextOnMouseOver(log); |
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
// combine properties to base prototype | |
function Combine(target, fns = []) { | |
let protoCopy = Object.assign(target.prototype); | |
let extendedProto = (Array.isArray(fns) ? fns : [fns]).reduce((proto, fn) => { | |
Object.getOwnPropertyNames(fn.prototype).forEach(prop => { | |
if(!(protoCopy.hasOwnProperty(prop))) { | |
proto[prop] = fn.prototype[prop]; | |
} |