Skip to content

Instantly share code, notes, and snippets.

View jashkenas's full-sized avatar

Jeremy Ashkenas jashkenas

View GitHub Profile
// How do you tend to declare variables in your code, when nested in an if / else?
// All three are functionally equivalent.
// (Ternary is cheating for these purposes.)
// Option 1:
function() {
if (condition) {
var attrs = a;
} else {
// Take the symmetric difference between a list of arrays. Only the elements
// present in one of the input arrays will remain.
_.symDifference = function() {
return _.reduce(arguments, function(memo, array) {
return _.union(_.difference(memo, array), _.difference(array, memo));
});
};
dc.ui.AnnotationEditor = Backbone.View.extend({
id : 'annotation_editor',
events : {
'click .close': 'close'
},
constructor : function(options) {
Backbone.View.call(this, options);
// Demonstration of dynamic super() calls.
// Because of JS reserved words, "ssuper()" is the method name,
// and is passed the current object, as well as the name of
// the current method.
function GreatGrandParent() {};
GreatGrandParent.prototype.method = function() {
console.log("In the GreatGrandParent.");
};
// Here is a proposal for minimalist JavaScript classes, humbly offered.
// There are (at least) two different directions in which classes can be steered.
// If we go for a wholly new semantics and implementation, then fancier classical
// inheritance can be supported with parallel prototype chains for true inheritance
// of properties at both the class and instance level.
// If however, we keep current JavaScript prototype semantics, and add a form that
// can desugar to ES3, things must necessarily stay simpler. This is the direction
// I'm assuming here.
// From: https://mail.mozilla.org/pipermail/es-discuss/2011-October/thread.html#17696
const className = superClass <| function(/*constructor parameters */) {
//constructor body
super.constructor(/*arguments to super constructor */);
this.{
//per instance property definitions
};
}.prototype.{
//instance properties defined on prototype
@jashkenas
jashkenas / wtf.coffee
Created August 25, 2011 19:12 — forked from gilesbowkett/wtf.coffee
why does this CoffeeScript not accurately translate this Ruby?
insert: (ball, column) ->
for row in @rows when row[column] is null
row[column] = ball
return
if a
if b
if c
d
e
f
dc.ui.Document = Backbone.View.extend({
initialize: function() {
this.model.bind('change', this._onDocumentChange);
this.model.bind('change:selected', this._setSelected);
this.model.bind('view:pages', this.viewPages);
this.model.notes.bind('add', this._addNote);
this.model.notes.bind('reset', this._renderNotes);
this.model.pageEntities.bind('reset', this._renderPages);
}
@jashkenas
jashkenas / searchhi
Created August 9, 2011 13:24
Trying CoffeeScript
highlightWord = (node, word) ->
if node.hasChildNodes
children = node.childNodes
highlightWord(item, word) for item in children
if node.nodeType == 3
tempNodeVal = node.nodeValue.toLowerCase()
tempWordValue = word.toLowerCase()