adequately good

decent programming advice

written by ben cherry




all posts from the year 2010

Saner HTML5 History Management

Hashchange

This event is quite simple. Whenever the window.location.hash property changes, by following a link, setting the property, editing the URL bar, or ...

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

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

JavaScript-Style Objects in Python

One of JavaScript’s most convenient features is the object syntax. Objects are so easy to work with. You’ve got a l...

Performance of === vs. ==

One particular weirdness and unpleasantry in JavaScript is the set of equality operators. Like virtually every language, JavaScript has the standard ==, !=, <, >, <=, and >= operators. However, == an...

Minimum Timer Intervals in JavaScript

I was talking with a co-worker today about the behavior of setTimeout and setInterval when given a small interval, like 0 or 1. The expectation would be tha...

Finding Improper JavaScript Globals

When I interview web developers, my first JavaScript question is usually the following:

What is the difference, in JavaScript, between x = 1 and var x = 1. Feel free to answer in as much ...

JavaScript Scoping and Hoisting

Do you know what value will be alerted if the following is executed as a JavaScript program?

var foo = 1;
function bar() {
	if (!foo) {
		var foo = 10;
	}
	alert(foo);
}
bar();

If it surprises you that the answer is “10”, then this one will probably really th...

Search/Replace in the DOM with jQuery

Ever had a need to to a text search/replace through the DOM? For articles in drafting on this blog I often use the form (TODO: somethi...

Preloading JS and CSS as Print Stylesheets

UPDATE: This technique has turned out to be dangerous in Chrome. It seems that Chrome will load the JS files into the cache, but then set an implied type="text/css" on them. This means that it will refuse to re-use them as Java...