2

I'm trying to make sense of script which is makeing heavy use of comma-separated expressions. For example:

popup_window != is_null && ("function" == typeof popup_window.close && popup_window.close(), popup_window = is_null);

If it's true that comma separation means "evaluate all of the following expressions, then produce the value of the final expression" (as in this SO answer), is this another form of an if statement?

Like:
"if popup_window is not null and popup_window.close is a method then call this method and set popup_window to null"

Question:
What does this statement mean and what's up with the comma separation? Is this supposed to be an if-statement?

miken32
  • 42,008
  • 16
  • 111
  • 154
frequent
  • 27,643
  • 59
  • 181
  • 333
  • It's just a clever (too clever) way of using AND to check a number of statements, and if they are all truthy it gets to the end where the comma is used as an operator, and if `window.close()` returns truthy (it returns the popup if one exists), the variable `popup_window` is set to `is_null`, which is probably `null` based on the statement. – adeneo Feb 10 '15 at 22:54
  • So and if statement fails along the way, the final expression is not executed? – frequent Feb 10 '15 at 22:56
  • 1
    Exactly, it's like `if ( truthy && falsy && truthy )`, the last one is never checked, as the second one is falsy, and it fails then and there. – adeneo Feb 10 '15 at 22:57
  • Too clever indeed... Thanks. Make it an answer? – frequent Feb 10 '15 at 22:58
  • 1
    That's a compiled, minified script. Get the source code, no one would ever write this by hand. – Bergi Feb 10 '15 at 22:59
  • 1
    when you write : if( a() && b() && c() ) {} a() will be tested, if a() returns true, b() will be tested, if b() returns true, c() will be tested when you write : if(a() && b() , c() ) {} a() will be tested, if a() returns true b() will be tested and, c() will always be evaluated http://jsfiddle.net/try2d0rj/ – Steven Feb 10 '15 at 23:00
  • 1
    I know people writing such code - so this doesn't need to be generated by minifiers ;) – Florian Loch Feb 10 '15 at 23:11

1 Answers1

2

It's a series of statements really

popup_window != is_null // if true, continue to the statement in the parenthesis
    && 
(
    "function" == typeof popup_window.close // if true continue to close the window
      && 
    popup_window.close()
    , popup_window = is_null // this is executed as long as "popup_window != is_null" 
);                           // is truthy, it doesn't depend on the other conditions

assuming is_null is really null, firstly popup_window must not be null.
Secondly, we can assume popup_window is another window, opened with window.open, as it supposedly has to have a close function, and it's a bit of a Yoda condition, but it can also be written

typeof popup_window.close === "function"

so popup_window has to have a close method to continue to the next step. The final step closes the popup window if it's not null, and if it has a close method.

popup_window.close()

so the other two conditions have to be truthy to get this far, there has to be a window, and it has to have a close method, and then that close method is called, and the window is closed.

And then there's the comma. From the docs

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

and we have

("function" == typeof popup_window.close && popup_window.close(), popup_window = is_null);

lets write it a little differently

(                          // ↓ must be thruthy .... ↓ for this to execute
   (typeof popup_window.close === "function" && popup_window.close())
   , popup_window = is_null
);      // ↑ unrelated, it's just another operand, seperated by comma

The trick here is that the last part, after the comma, is always executed, as all operands seperated by the comma is evaluated.

This means that if popup_window is not is_null, popup_window is explicitly set to is_null, regardless of the second condition.

The second condition is also only exectuted it popup_window is not is_null, it then checks if there is a close() method, and closes the window, the statement after the comma is unrelated to the outcome of that condition.

To write it even simpler (the way it should have been written IMO)

if ( popup_window != is_null ) {

    if ( typeof popup_window.close === "function" ) {
        popup_window.close();
    }

    popup_window = is_null;

}
adeneo
  • 312,895
  • 29
  • 395
  • 388