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;
}