-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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, $); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function $( selector, context ) { | |
return [].slice.call( ( context || document ).querySelectorAll( selector ) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JQueryInheritor extends jQuery | |
constructor: ( arg ) -> | |
jQuery.fn.init.call( this, arg ); | |
myMethod = -> | |
# do something |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bill extends Backbone.Model | |
initialize: ( id ) -> | |
@id = id; | |
@_when = $.when( @getAmendments(), @getVotes() ) | |
then: ( callback ) -> | |
@_when.then( callback ) | |
@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require("bluebird"); | |
var fs = require("fs"); | |
Promise.promisifyAll(fs); | |
// fs.readFileAsync("file.js", "utf8").then(...) | |
var readFileTree = function ( root ) { | |
var results = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function applyConstructor ( ctor, args ) { | |
var instance; | |
function C () {} | |
C.prototype = ctor.prototype; | |
var instance = new C(); | |
ctor.apply( instance, args ); | |
return instance; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Singleton = (function() { | |
var instance; | |
return function () { | |
if ( !this instanceof Singleton ) { | |
return new Singleton(); | |
} | |
if ( !instance ) { | |
instance = this; | |
} | |
return instance; |