adequately good

decent programming advice

written by ben cherry



all articles from the year 2010

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 ...

Debugging Closures and Modules

The most common complaint with using closures to keep private variables in JavaScript is that it makes debugging harder. This complaint definitely holds water, and the loss of easy debugging and inspection using the Module Pattern is a serious concern. This is where one of JavaScript's non-standard but well-supported features comes in: the debugger statement. In this article, I'll introduce this statement, and show how I ...

Object-to-Primitive Conversions in JavaScript

Like most object-oriented programming languages, JavaScript provides built-in ways to convert between objects and primitive values, by way of the special toString and valueOf methods. This article will cover the basics of these methods, but then dive into the details of how this stuff really works, bad stuff, performance, and browser support.

Types and Primitives

To understand this article, you'll need to understand the difference between primitive ...

go next