adequately good

decent programming advice

written by ben cherry



Replacing `setTimeout` Globally

Sometimes, you might want to overwrite built-in global methods like setTimeout and setInterval. If you try, you might find that it's much harder than you think to accomplish this in every browser, particularly if you ever want to find the originals again. After a lot of painful experimentation, I think I have a definitive solution that works in all browsers with minimal side-effects.

Failed Approaches

I started ...

Saner HTML5 History Management

Included in the laundry list of new features that are descending on the world of web development with HTML5 are two quite nice ones: History Management and the hashchange event. These two features allow for much richer and faster JavaScript applications. Let's start with a quick overview of what these provide.

Hashchange

This event is quite simple. Whenever the window.location.hash property changes, by following a ...

Writing Testable JavaScript

The engineering culture at Twitter requires tests. Lots of tests. I haven't had formal experience with JavaScript testing before Twitter, so I've been learning a lot as I go. In particular, a number of patterns I used to use, write about, and encourage have turned out to be bad for writing testable code. So I thought it would be worthwhile to share a few of the ...

Spying Constructors in JavaScript

When writing unit-tests for code, a common technique is spying, where you set expectations on a method's invocation, run some code, and verify that the method was invoked as expected. This is pretty straightforward. Here's a simple example using JsMockito:

function foo(a) { return a; }
foo = spy(foo);
foo(1);
verify(foo)(1); // verified!
verify(foo)(2); // never run

Here, we're spying on the foo ...

go next