content top

Updating Wikipedia’s Decorator Pattern JavaScript example

Updating Wikipedia’s Decorator Pattern JavaScript example

I was looking at the Decorator Pattern on Wikipedia this morning when I noticed the sample code had at least two flaws. The code that was there at the time is: // Class to be decorated function Coffee() { this.cost = function() { return 1; }; } // Decorator A function Milk(coffee) { this.cost = function() { return coffee.cost() + 0.5; }; } // Decorator B function Whip(coffee) { this.cost = function()...

Read More

A better jQuery.inArray function

A better jQuery.inArray function

I recently found myself wanting to get the index of a Date object in an array of Dates. My initial thought was to use jQuery.inArray. However, I quickly realized that it wouldn’t work, and the reason is because Date objects (and all other objects in JavaScript) can’t be compared using the == or === operator. Well, they can be compared, but the object will only ever be equal to itself, not to a different object with identical...

Read More

What Have You Tried?

What Have You Tried?

I don’t plan on getting into the habit of linking to other blog posts. However, I am making an exception because it’s about a fundamental problem in development that I see too often (and catch myself doing as well), so it deserves to be linked to by as many sources as possible. Trust me, click the link: What have you tried?

Read More

jQuery Pre-Event Plugin

jQuery Pre-Event Plugin

I wrote this plugin a long time ago, but just improved upon it and updated it to work with jQuery 1.7.1. It’s probably easiest to talk about the usage by describing the problem it solves. The problem. Let’s say I have a “Contact Us” form, and when the user clicks the Submit button, I want to first validate that all of the information is correctly entered. My code would probably look something like...

Read More

Accessing bound data with Handlebars

Accessing bound data with Handlebars

Yesterday I built a calendar control that uses Handlebars to render the calendar. As I started writing my click event handler for the calendar’s table cells, I realized that inside the event handler I wouldn’t have access to the object that was used to render the cell. That object had a property that was a Date, and when the cell is clicked, I need to get that Date back so I can do something with it. I headed over to Stack...

Read More

Welcome to my blog!

Welcome to my blog!

I’ve been staring at this stupid “Add New Post” screen for about 2 hours now, constantly allowing my attention to drift to something else until I remember “oh yeah, I want to write that blog post.”  So I’m just going to stop trying to craft the perfect “hello world” blog post and just get this damn thing done with. This blog will mostly be about programming, primarily in JavaScript since...

Read More
content top