Skip to content

Instantly share code, notes, and snippets.

@sebmarkbage
sebmarkbage / Move.md
Last active August 29, 2015 14:01
Object Rest Destructuring and Spread Initializer (ES7 proposal)
@rwaldron
rwaldron / emitter.js
Last active December 15, 2015 07:39
Simple Event Emitter class that uses a Symbol for event binding storage
// In Chrome Canary, with Experimental JavaScript enabled...
(function( exports ) {
// Create a reusable symbol for storing
// Emitter instance events
var sym = new Symbol();
function Emitter() {
this[ sym ] = {
@johan
johan / README.md
Last active April 18, 2025 15:35
A micro-library (4k minified) for DRY:ing up the boring boilerplate of user scripts.

The fun part of user scripting is deciding what happens. The boring part is scavenging the DOM for bits of templated data, or elements you want to mod.

Have on.js do it for you!

@jed
jed / rendering_templates_obsolete.md
Created October 19, 2012 05:07
Rendering templates obsolete

(tl;dr DOM builders like [domo][domo] trump HTML templates on the client.)

Like all web developers, I've used a lot of template engines. Like most, I've also written a few of them, some of which even [fit in a tweet][140].

The first open-source code I ever wrote was also one of the the first template engines for node.js, [a port][node-tmpl] of the mother of all JavaScript template engines, [John Resig][jresig]'s [micro-templates][tmpl]. Of course, these days you can't swing a dead cat without hitting a template engine; one in eight packages on npm ([2,220][npm templates] of 16,226 as of 10/19) involve templates.

John's implementation has since evolved and [lives on in Underscore.js][underscore], which means it's the default choice for templating in Backbone.js. And for a while, it's all I would ever use when building a client-side app.

But I can't really see the value in client-side HTML templates anymore.

function range(start, end, step) {
step = Math.floor(Math.abs(step)) || 1;
if (typeof end == 'undefined') {
end = start;
start = 0;
}
else if (start > end) {
var s = start;
start = end;
end = s;
@johan
johan / jQuery.shortNumber.coffee
Created October 5, 2012 20:07
jQuery plugin to show arbitrarily large numbers in at most four characters, i e: 80087 => 80k
###
Usage: $(node).shortNumber(4711) sets the contents of `node` (to "4.7k")
$(node).shortNumber() reads that actual number back from the node
If you want to pre-shorten some DOM node with a value on the server side,
be sure to set the data-shortNumber="4711" or whatever number on it, too:
Likes: <span data-shortNumber="<% _num %>"><% short_number _num %></span>
(To just format arbitrary numbers to tiny strings use shortNumber(4711).)
@dherman
dherman / beget.js
Created July 28, 2012 18:13
What everyone kinda wishes Object.create had been
Object.beget = (function() {
var create = Object.create,
defineProperty = Object.defineProperty;
// see https://gist.github.com/3194222
var hasOwn = Object.prototype.hasOwnProperty.unselfish();
return function beget(proto, own) {
var result = create(proto);
@Gozala
Gozala / base.js
Created April 5, 2012 17:19
Callable objects prototype implementation
/* vim:set ts=2 sw=2 sts=2 expandtab */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { obscure, extend } = require('./heritage');
const Base = extend(undefined, obscure({
@DavidBruant
DavidBruant / Client.js
Created April 1, 2012 21:57
Protected protocol
var d = new Person('David');
console.log('d.name:', d.name);
setTimeout(function(){
console.log('d age', d.getAge());
}, 500);
console.log('d instanceof Person', d instanceof Person);
console.log('d instanceof ComputerSavvyPerson', d instanceof ComputerSavvyPerson);
var db = new ComputerSavvyPerson('David Bruant', 'JavaScript');
console.log('db.name:', db.name);
@kriskowal
kriskowal / namespace.js
Created March 16, 2012 00:14
Prototypical name spaces with WeakMap
function Namespace(common) {
var map = WeakMap();
common = common || null;
function ns(object) {
if (Object(object) !== object)
return common;
if (!map.has(object)) {
var prototype = Object.getPrototypeOf(object);
map.set(object, Object.create(ns(prototype)));
}