Skip to content

Instantly share code, notes, and snippets.

var Oldsmobile = function(year, color) {
// private members:
var allowed = ['red', 'black', 'tan'];
var defaultColor = 'black';
// manipulate instance initialization by accessing constructor methods:
Oldsmobile.addAllowed(allowed, 'blue');
// privleged members:
this.color = allowed.find(_color => _color === color) ? color : defaultColor;
const generate2dBoard = (cols, rows) => {
let board = generate2dArray(cols, rows);
let roomSpecs = {
heightMin: 5,
heightMax: 6,
widthMin: 5,
widthMax: 6,
paddingMin: 1,
paddingMax: 4
};
@jpenney1
jpenney1 / Deep copies Matter... why?
Created April 16, 2018 05:05
Checkout out how the deep clone changes does not change the reference to the object in numsRef[4]. Uncomment line 16, and see how the assignment copy assigns even references, allowing for the original objects to be mutated through the 'copies'.
let numsRef = {
1: 1,
2: 2,
3: 4,
4: { 1: 'hi' }
};
let deepObj = {
numbers: numsRef
};
@jpenney1
jpenney1 / gamepad snippet
Created May 9, 2018 20:04
with this snippet, you can add an event listener to any webpage which will log the button pressed to the console, along with its entire availbale buttons array and index at which it was found. Anyone know how if there's a naive way to determine which button in the array is which?
var gameLoop = function(res) {
var gamepad = navigator.getGamepads();
navigator.getGamepads()[0].buttons.forEach(function(button, i, buttons) {
if(button.pressed) console.log('pressed: ', buttons.slice(),' i: ', i, buttons[i])
});
requestAnimationFrame(gameLoop)
}
window.addEventListener('gamepadconnected', gameLoop)
// src: https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
var ARGS = process.argv.slice(2),
COMPONENT_NAME = ARGS[0],
COMPONENT_IS_SYSTEM = ARGS[1] == 'true',
JSX_SUUBPATH = (COMPONENT_IS_SYSTEM) ? 'systems' : 'components',
COMPONENT_DIR = `./${JSX_SUUBPATH}/${COMPONENT_NAME}`,
fs = require('fs')
if(!COMPONENT_NAME) {
@jpenney1
jpenney1 / pureBounce.js
Created March 29, 2019 21:36
Run this code in an html script with an index element in the body, type anything into the input and see an alert after two seconds of inactivity
const debounce = (callback, wait) => {
let timeout = null
return (...args) => {
const next = () => callback(...args)
clearTimeout(timeout)
timeout = setTimeout(next, wait)
}
}
const inputEl = document.getElementsByTagName('input')[0]
const copy = item => {
return !item ? item
: Array.isArray(item) ? deepArrayCopy(item)
: Object.prototype.toString.call(item).includes('Date') ? new Date(item.getTime())
: typeof item === 'object' ? deepCopy(item)
: item
}
const deepCopy = obj => {
const objKeys = Object.keys(obj)
@jpenney1
jpenney1 / pureTrampoline.js
Last active April 5, 2021 18:11
Custom efficient tail end optimization for recursive functions in JavaScript utilizing the trampoline pattern
const trampoline = fn => (...args) => {
let res = fn(...args)
while(typeof res === 'function'){
res = res()
}
return res
}
/*call trampoline with a function that returns either the resolved
value (if termination parameters are met) or the next function call
@jpenney1
jpenney1 / Flux Architecture Base (Vanilla JS)
Created April 16, 2018 05:02
Vanilla Javascript flux implementation. Easy to understand and copy into your own project, modifying and extending as necessary.
const createStore = (reducer, initialState) => {
const subscribers = [];
let currentState = initialState;
return {
getState: () => currentState,
subscribe: fn => {
subscribers.push(fn);
fn(currentState);
},