Archive for the ‘JavaScript’ Category

Semicolon insertion finally caught me

Thursday, April 8th, 2010

While debugging a caching class today, I realized that I had finally been caught by semicolon insertion.

Here’s the original code cut from a larger object literal

isValid: function(hash) {
    return
        // Is cached AND
        typeof this.entries[hash] != "undefined" &&
        // Cache doesn't expire (e.g.: -1) OR hasn't expired yet
        (this.entries[hash].expires < 0 || this.entries[hash].expires > new Date().getTime());
}

After about 10 minutes and a gratuitous amount of debug output, I realized that this function was returning `undefined`.  “Nonsense,” I said to myself, “it’s returning a boole…wha…wait. Dammit.”

In my effort to document and format at the same time, I dropped the expression to the next line after the `return`.  Being that `return` by itself on a line is a complete and valid expression, JavaScript’s automatic semicolon did its thing and ended the statement there and my conditional expression was never evaluated.

Wrapping the whole conditional expression in parenthesis fixed the problem by invalidating the `return` line as a whole statement.

isValid: function(hash) {
    return (
        // Is cached AND
        typeof this.entries[hash] !== "undefined" &&
        // Cache doesn't expire (e.g.: -1) OR hasn't expired yet
        (this.entries[hash].expires < 0 || this.entries[hash].expires > new Date().getTime())
    );
}

Let that be a lesson to us all. I suppose it was bound to happen at some point.

XBC: Cross-Browser Consistency

Saturday, July 5th, 2008

Author’s note: This article was originally posted at Frye / Wiles, and has been re-posted here for consolidation.

When we talk about topics such about CSS, JavaScript, and sometimes even certain image formats (png24, I’m looking at you), and how they render in a client’s browser, we always, or should always also consider cross-browser behavior. This behavior entails many things: CSS rendering, the availability of CSS specific attributes, whether or not the DOM interface will be the same, general JavaScript behavior — the list goes on and on. And, since most discussions about CSS and JavaScript (at least the ones that I am having) also concern this variable nature, I’m coining (maybe I’m the first) a new term to put all of this into a handy little phrase, “Cross-Browser Consistency,” or, in typical programmer fashion, simply, “XBC.”

Let’s take a brief moment to establish a more exact meaning for this phrase. As you may well be aware, the industry commonly talks about cross-browser support, so we’ll differentiate between support and consistency, as well as defining what cross-browser really means, and some other tidbits as to boot.

(more…)