Skip to content

Instantly share code, notes, and snippets.

(function(root, factory) {
// Set up Module appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'exports'], function(_, exports) {
// Export global even in AMD case in case this script is loaded with others that may still expect a global "Module".
root.Module = factory(root, exports, $);
});
@bttmly
bttmly / tiny-bling.js
Created June 29, 2014 19:59
Miniscule DOM selection function that returns arrays
function $( selector, context ) {
return [].slice.call( ( context || document ).querySelectorAll( selector ) );
}
@bttmly
bttmly / extends-jquery.coffee
Last active August 29, 2015 14:03
extending the jQuery class
class JQueryInheritor extends jQuery
constructor: ( arg ) ->
jQuery.fn.init.call( this, arg );
myMethod = ->
# do something
var createObservedObject = function(callback) {
var obj = {};
var args = [].slice.call(arguments, 1);
for(var i in args) {
var thisPropertyName = args[i];
(function(thisPropertyName) {
obj['_' + thisPropertyName] = undefined;
Object.defineProperty(obj, thisPropertyName, {
get: function() {
class Bill extends Backbone.Model
initialize: ( id ) ->
@id = id;
@_when = $.when( @getAmendments(), @getVotes() )
then: ( callback ) ->
@_when.then( callback )
@
var Promise = require("bluebird");
var fs = require("fs");
Promise.promisifyAll(fs);
// fs.readFileAsync("file.js", "utf8").then(...)
var readFileTree = function ( root ) {
var results = [];
function toArray(args) {
return [].slice.call(args);
}
function autocurry(fn) {
var len = fn.length;
var args = [];
return function next() {
args = args.concat(toArray(arguments));
return (args.length >= len) ?
@bttmly
bttmly / slide1.md
Created August 21, 2014 02:07
Iteration presentation

Why use Array.prototype iterators?

  • Expressive They say something about the purpose and result of the iteration.

  • Contained They don't have side effects if used properly. All relevant stuff occurs within the callback.

  • Scoped They provide automatic function scope for each iteration which eliminates a common type of error.

function applyConstructor ( ctor, args ) {
var instance;
function C () {}
C.prototype = ctor.prototype;
var instance = new C();
ctor.apply( instance, args );
return instance;
}
@bttmly
bttmly / singleton.js
Created August 28, 2014 22:41
Singleton
var Singleton = (function() {
var instance;
return function () {
if ( !this instanceof Singleton ) {
return new Singleton();
}
if ( !instance ) {
instance = this;
}
return instance;