Question

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.

Was it helpful?

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.

OTHER TIPS

!0 == clickEnabled && (clickEnabled = !1

Part seems to be saying: If clickEnabled, disable clicking - prevent double clicking, likely.

, a(this).hasClass("iT_radio") 

The comma should be interpreted as a list, so as well a setting clickEnabled == false, it will do the next part which is to check for iT_radio presence.

? a(this).hasClass("iTon") 

If iT_radio is present, then check is iTon is present as well.

? clickEnabled = !0 

If iT_radio and iTon present, re-enable clicking

: e(a(this), !0)

If iT_radio is present, but iTon is not then e(a(this), true). I do not know jquery, so unsure what the e function is.

: e(a(this)));

If iT_radio is not present, do this e(a(this)). Again do not know the purpose of the function e.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top