Skip to content

Instantly share code, notes, and snippets.

@matthewrobb
matthewrobb / array-like.mjs
Last active September 5, 2019 18:20
consumable-array.mjs
const {
defineProperties,
getOwnPropertyDescriptors
} = Object;
/**
* A root Array-like "class" compatible with both es5 and es6 styles
*/
export function ArrayLike() {
if (!(this instanceof ArrayLike)) {
;(function() {
"use strict";
// Utility class to enable extension of objects that cannot use
// normal class based inheritance e.g. custom functions
//
// Simply put, it adds a mixin static that contains all the property
// descriptors that class intends to pass on
class Mixable {
static mixin(target) {
@matthewrobb
matthewrobb / index.html
Created November 4, 2018 17:16
Mithril + Mobx Mithril + Mobx // source https://jsbin.com/jesebuv
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Mithril + Mobx">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Mithril + Mobx</title>
<script src="https://unpkg.com/mithril/mithril.js"></script>
</head>
<body>
'use strict';
export function free(fn) {
return function() {
return this === undefined ? fn.apply(undefined, arguments) : fn.call(undefined, this, ...arguments);
}
}
export function freeRight(fn) {
return function() {
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
@matthewrobb
matthewrobb / inheritize.js
Last active December 27, 2015 00:39
Recursive object descent and prototype reassignment
function inheritize(root){
Object.keys(root).forEach(function(key){
var branch = root[key];
if (typeof branch === "object") {
Object.keys(branch).forEach(function(key){
var leaf = branch[key];
if (typeof leaf === "object" && typeof root[key] === "object") {
// In the HOPEFULLY near future this would be replaced with:
// Object.setPrototypeOf(leaf, root[key]);
leaf.__proto__ = root[key];
const Shade = Shades;
const { Content, Slot } = Shades;
function PageFooter(props) {
return (
<footer>
{props.children}
</footer>
);
}
@matthewrobb
matthewrobb / bindAsync.js
Last active December 17, 2015 22:09
Async function bind.
var bindAsync = function(fn, ctx) {
return function() {
setTimeout(fn.bind.call(fn.apply, fn, ctx || this, arguments), 0);
}
};
@matthewrobb
matthewrobb / gist:5272889
Created March 29, 2013 19:06
Simple mixin function with prototype support
function mixin(receiver, source) {
var descriptors = {};
for(var name in source) {
if(source.hasOwnProperty(name)) {
if(typeof receiver == "function" && name == "prototype") {
receiver.prototype = mixin(Object.create(receiver.prototype), source.prototype);
} else {
descriptors[name] = Object.getOwnPropertyDescriptor(source, name);
}
}
@matthewrobb
matthewrobb / robot.js
Created December 4, 2012 22:39
Phenbot
function Robot(robot) {
this.bots.push(this);
};
Robot.prototype = {
constructor: Robot,
bots: [],
onIdle: function(ev) {
ev.robot.clone();