Search code examples
javascriptqtip

Assigning JavaScript primitives to their named equivalent variable like "constants"


I was looking at the source code to qTip 2 and saw the following:

// Munge the primitives - Paul Irish tip
var TRUE = true,
   FALSE = false,
    NULL = null;

I can't come up with a reason you should ever do this, and have a strong feeling that it would just encourage bad coding habits. Say a developer makes a typo in a Yoda condition like if (TRUE = someCondition()), then TRUE could very well end up actually meaning false, or you might end up assigning someObject to NULL.

I guess I'm just wondering if there's some redeeming quality for this practice that I'm missing, or if this is just a plain old Bad Idea™


Solution

  • The goal of this is just to improve compression, Paul Irish himself calls it as an "Anti-Pattern".

    He describes it as "Good for compression and scope chain traversal" on the following presentation:

    On scope chain traversal, we won't see any improvement on literals as null, false, true, since the scope chain is not inspected, they are just literals.

    On other identifiers as undefined or windows the scope chain traversal is indeed inspected.

    Paul Irish Anti-patterns slide 55