Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: false if expression continues execution of if scoped code

Tags:

javascript

I am experiencing some of the weirdest behavior of Javascript.

Even though the expression in the if statement evaluates to false, my code inside the if block keeps executing.

Here is the code snippet:

if (view.leftCols !== null) {
    var leftCols = view.leftCols.split(',');

    for (var lc = 0; lc < leftCols.length; lc++) {
        var lcv = leftCols[lc].split(':');
        var lcol = lcv[0];

        Array.add(allCols, lcol);

        try {
            var lwidth = lcv[1] - MyWorkGrid.getColWidth(lcol);

            if (lcol === 'Edit') {
                if ($.browser.msie && parseInt($.browser.version) <= 8) {
                    lwidth = 23;
                }
            }

            // this if statement fails.
            if (lwidth > 0) {
                grid.SetWidth(lcol, lwidth);
            }
        } catch (e) {
        }

         grid.MoveCol(lcol, 0, 1, 1);
    }
}

Here is my Firebug screenshot:

Firebug

Any ideas?

like image 645
Moon Avatar asked Feb 19 '12 16:02

Moon


1 Answers

Despite the line being highlighted in Firebug, are you sure it's really being executed? I've seen this behavior in Firebug (and early versions of the Chrome debugger) where they would sometimes highlight a line of code as though they'd stepped to it, without that line actually getting executed.

So I'd insert something unambiguous (like a console.log("lwidth = " + lwidth); into the if statement's body to see if it really executes. The answer may be that Firebug is just highlighting/showing that line as the current one incorrectly without the code thereon actually being run.

like image 56
T.J. Crowder Avatar answered Oct 28 '22 15:10

T.J. Crowder