This file contains 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
// Succinct, does not create strictly unnecessary variables | |
// but not easy to read | |
if((a.notes && a.notes.trim() != "") && (b.notes && b.notes.trim() != "")) { | |
skip = true; | |
console.log('too many notes - skipping'); | |
} | |
// Verbose, creates two vars which are not really needed | |
// but it's instantly clear what the 'if' is doing | |
var aHasNotes = (a.notes && a.notes.trim() != ""); |
This file contains 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
/**/ | |
someCode(); | |
goesHere(); | |
lotsOfCode(); | |
/**/ | |
// With removal/addition of just one char we can toggle commenting of the whole block: | |
/** | |
someCode(); |
This file contains 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
define(['require', 'dependency1', 'dependency2'], function (require, dependency1, dependency2) { | |
return function () {}; | |
}); |
This file contains 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
/*jslint node:true*/ | |
// Usage curl localhost:8080/http://example.com | |
// - proxies the request, adding Access-control-allow-origin: * and stripping Origin and Referer headers and fixing Host | |
"use strict"; | |
var request = require("request"), | |
http = require("http"), | |
url = require("url"); | |
http.createServer(function proxy(req, res) { |
This file contains 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
// A way to use module pattern along with new keyword | |
function Foo() { | |
this.baz = 4; | |
function bar() { | |
return this.baz; | |
} | |
return { | |
"bar":bar.bind(this) | |
}; | |
} |
This file contains 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 SomeBuggyInstantiatedObject() { | |
this.name = "bob"; | |
someElement.onclick = function() { | |
alert(this.name); // undefined, because this context is the click event | |
} | |
} | |
function SomeSuperCoolInstantiatedObject() { | |
var that = this; | |
this.name = "bob"; |
This file contains 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
// document.body.scrollTop = 1; // uncomment this line and it works every time. | |
console.log(daysTop); // 15141 | |
console.log($(document).height()); // 54005 | |
document.body.scrollTop = daysTop; | |
console.log(document.body.scrollTop); // 0, sometimes, in chrome (and the page doesn't move to 15141).... | |
/* | |
if I attach the above code to a click handler, then when the bug occurs, it occurs on | |
every click until I manually scroll the page, then it starts working. |
This file contains 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
// This is called whenever any data changes that affects the weekly totals | |
// What we do in that case is modify our local representation of the weekly data and emit an event | |
// This way, modules can listen for that event and respond to it without having to wait for | |
// the server to generate new weekly totals |
This file contains 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
// Hide all miniapps. | |
// for promise().done() explanation, see http://api.jquery.com/promise/#example-1 | |
// tldr; if you put a .complete() callback in an animation which matches > 1 element | |
// the callback will be called once per matching element | |
// We don't want that, we want the callback to run once all animations are complete. | |
$('.miniapp').slideUp("fast").promise().done(function runMeWhenAllSlideupsAreComplete() { |
This file contains 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
// It would be very simple to write this function to sort-of mimic let; | |
lett({ | |
'blockScopeVar': 4 | |
}, function (blockScopeVar) { | |
//blockScopeVar === 4 // true | |
}); | |
// Then when you want to change the code to actually use let, it's fairly easy; | |
// just write your let declarations and rip out the callback's innards; |