Search code examples
javascriptjqueryternary

What in the world is this embedded ternary executing?


I have inherited some absolutely god-awful code from a vendor and between being a relative rookie in Javascript and the atrocious way this is written, I have managed to stump the entirety of my office with what this code is supposed to mean. Can someone help out by rewriting the following as an embedded IF statement or something a little more readable so I can modify the flow of the code a bit?

a("label.iClass").click(function () {
        !0 == clickEnabled && (clickEnabled = !1, a(this).hasClass("iT_radio") 
        ? a(this).hasClass("iTon") 
          ? clickEnabled = !0 
          : e(a(this), !0) 
        : e(a(this)));

        return !1;
}

clickEnabled is a property on the custom object the vendor has provided. It is a boolean, initially defined as !0. No, I have no idea why they decided negating integers was preferable to just using a boolean.

The two pieces throwing me for the biggest loop are the binary AND preceding a variable assignment, and the comma placed directly after the assignment going into another function call. Any input on what that could mean would be most appreciated, too.


Solution

  • a("label.iClass").click(function () {
        if (clickEnabled == true) {
            clickEnabled = false;
            if (a(this).hasClass("iT_radio"))
                if (a(this).hasClass("iTon"))
                    clickEnabled = true;
                else 
                    e(a(this), true);
            else 
                e(a(this)));
        }
        return false;
    });
    

    Where a is presumably the jQuery function, and e is some other function in the code.