Skip to content

Instantly share code, notes, and snippets.

@user24
user24 / variable-names.js
Last active December 14, 2015 15:58
Code Clarity by hoisting logic into nicely named variables
// 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() != "");
@user24
user24 / toggle.js
Created January 7, 2014 21:22
Toggle Comments
/**/
someCode();
goesHere();
lotsOfCode();
/**/
// With removal/addition of just one char we can toggle commenting of the whole block:
/**
someCode();
define(['require', 'dependency1', 'dependency2'], function (require, dependency1, dependency2) {
return function () {};
});
@user24
user24 / accessproxy.js
Last active June 4, 2018 15:49
Proxy connections, opening up Access-control-allow-origin
/*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) {
// 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;