Skip to content

Instantly share code, notes, and snippets.

View MikeDigitize's full-sized avatar
👋
Howdy

Mike Chadwick MikeDigitize

👋
Howdy
View GitHub Profile
@MikeDigitize
MikeDigitize / returns are useful!
Last active January 6, 2016 22:08
Helpers for beginners to JavaScript at AO.com
// 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;
@MikeDigitize
MikeDigitize / Literals vs Constructors
Last active January 21, 2016 21:59
Helpers for beginners to JavaScript at AO.com
/*
Constructor vs Literal
*/
// Array
var foo = new Array(); // constructor
var bar = []; // literal
// difference?
// constructor - inconsistent API
@MikeDigitize
MikeDigitize / Write a lottery ball generator
Last active November 28, 2016 23:50
Some coding challenges for the AO front end team who are learning JavaScript
function lotto() {
function getBalls() {
var random = getRandom(1,59);
if(!balls.includes(random)) {
balls.push(random);
}
if(balls.length < 6) {
return getBalls();
}
else {
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)) {
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);
var PubSub = (function() {
let listeners = {};
return {
listeners,
subscribe (evt, fn) {
if(!this.listeners.hasOwnProperty(evt)) {
this.listeners[evt] = [];
}
this.listeners[evt].push(fn);
return () => {
const capitalise = f => replace(/\b[a-z]/g, f => f.toUpperCase());
const lowercaseilise = f => replace(/\b[A-Z]/g, f => f.toLowerCase());
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);
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);
@MikeDigitize
MikeDigitize / ES2015 Class Mixins
Last active September 7, 2016 12:45
How to bolt on mixins to a base class in ES2015
// 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];
}