2

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.

felipekm
  • 2,820
  • 5
  • 32
  • 42
  • 9
    This looks like minified code. – elclanrs Jan 15 '14 at 19:15
  • This is what the vendor sent us after we _specifically asked them for unminified code._ – user3199613 Jan 15 '14 at 19:17
  • 1
    Because they probably just beautified it from minified code. `!0` is `true` and `!1` is `false`. The assignment looks like `clickEnabled = clickEnabled || ...` but funkier. – elclanrs Jan 15 '14 at 19:18
  • That is what I suspected, but it doesn't help me too much in understanding the flow of this awful mess. Do you have any insights there? – user3199613 Jan 15 '14 at 19:21
  • 1
    I'd tell them to send the real unminified code. This is on their end, you shouldn't be expected to read minified code. – elclanrs Jan 15 '14 at 19:22
  • [Comma Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2FComma_Operator): "The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand." – Andrew Morton Jan 15 '14 at 19:22
  • Comparing a boolean with `!0` - ouch. Probably the unminified code is not much better. – Bergi Jan 15 '14 at 19:33

2 Answers2

3
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.

cookie monster
  • 10,671
  • 4
  • 31
  • 45
  • 2
    Guhh, beat me by a minute... This link might help: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/ – Josh Jan 15 '14 at 19:24
  • @thg435: The `== true` is required in order to be an exact translation. – cookie monster Jan 15 '14 at 19:42
  • @cookiemonster: maybe, but it's doesn't make any sense. – georg Jan 15 '14 at 19:43
  • @thg435: If we could guarantee that `clickedEnabled` will always be boolean, then you're right. We just don't have enough code to make that determination. – cookie monster Jan 15 '14 at 19:44
  • Holy God, thank you. Once I understood what was going on and read Josh's link I was able to modify what I needed in a few seconds. Thank you very, very much. – user3199613 Jan 15 '14 at 19:45
0
!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.

Chris
  • 2,655
  • 2
  • 18
  • 22