Skip to main content

Posts

Showing posts from July, 2012

The importance of Kerning

I'm not that great of a graphic designer, and I'm even less skilled when it comes to typography. But I know the basics, and this is a great (and hilarious) example of what kerning is (in short: the distance between letters) and why it's important (in this case, "click" looks like "dick"). I can't take credit for this image. It was a retweet of a retweet originally posted by @jbrownridge .

Use parseInt Correctly

Something else I just learned today. I've been happily using parseInt all this time, and I've been doing it wrong . I never really thought much about it. "Ok, let's change this string to an integer. parseInt(string) Bang. Done." But then I got a warning when compiling my code with Google's Closure Compiler saying that I needed two parameters. Wait, what? Turns out, there is a second parameter for the Radix that you should always fill in . This makes sense, of course, but apparently I assumed everyone used base ten. Always. That was pretty dumb, and here's why: If the input string begins with "0x" or "0X", radix is 16 (hexadecimal). If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt. If the input string begins with any ot

Copying an Object in JavaScript

The Setup var default = { param1: "one", param2: "two", param3: [] }, working = default; // Fill form fields from working data // User modifies form fields // Save form fields in working data // Perform calculation based on working data // User clicks a button to reset the form to default state function reset(){ working = default; // Fill form fields from working data // Perform calculation based on working data } The Problem The form was not being reset to the default data, but instead did not change at all. When I debugged, I noticed that the data in default was identical to the data in working , even though I never assign any values to default after initialization. The Cause Little did I know, that assigning an object in JavaScript actually only assigns the reference, and does not create a new copy of the object. The Solution The object needs to be copied, rather than simply assigned. Based on a quick Google search, there is no bui