Skip to content

Instantly share code, notes, and snippets.

// 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)
};
}
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";
// 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 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
// 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() {
// 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;
$('#calendar .barIcons > .barIcon').each(function (i, icon) {
var left;
icon = $(icon);
left = parseInt(icon.position().left, 10);
alert(left); // increases each time
return; // Comment this and now the alert is zero each time.
icon.css({
"position": "absolute"
});
@user24
user24 / gist:1187386
Created September 1, 2011 21:48
naive debug function
// Crappy naive debug wrapper function which sucks
function debug(msg) {
console.log(msg),
}
@user24
user24 / gist:1187400
Created September 1, 2011 21:56
debug in use
var debug = true;
function parseJSON(json) {
debug && console.log('parsing json');
if(typeof json == 'string') {
json = JSON.parse(json);
}
debug && console.log('got json', json);
}
@user24
user24 / gist:1187424
Created September 1, 2011 22:09
Toggle Comments
// This block is commented out:
/**
bigBlockOfCode();
lalala();
fooBar();
if(someStuff()) {
doExtraThings();
}
/**/