逗号分隔的表达式可以像Javascript中的if语句一样使用吗?

逗号分隔的表达式可以像Javascript中的if语句一样使用吗?,javascript,expression,comma,Javascript,Expression,Comma,我试图理解大量使用逗号分隔表达式的脚本。例如: popup_window != is_null && ("function" == typeof popup_window.close && popup_window.close(), popup_window = is_null); 如果逗号分隔表示“计算以下所有表达式,然后生成最终表达式的值”(如中所示),那么这是If语句的另一种形式吗 比如: “如果popup_window不为null且popup_windo

我试图理解大量使用逗号分隔表达式的脚本。例如:

popup_window != is_null && ("function" == typeof popup_window.close && popup_window.close(), popup_window = is_null);
如果逗号分隔表示“计算以下所有表达式,然后生成最终表达式的值”(如中所示),那么这是If语句的另一种形式吗

比如:
“如果popup_window不为null且popup_window.close是一种方法,则调用此方法并将popup_window设置为null”

无论如何,我不确定我是否理解语法

问题
这句话是什么意思?逗号分隔是怎么回事?这应该是if语句吗


谢谢

这是一系列的陈述

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
假设
为空
确实为
,首先
弹出窗口
不能为空。
其次,我们可以假设
弹出窗口
是另一个窗口,用
窗口打开。打开
,因为它应该有一个
关闭
功能,这有点像Yoda条件,但也可以写入

typeof popup_window.close === "function"
因此,
弹出窗口
必须有一个
关闭
方法才能继续下一步。 如果弹出窗口不为空,并且具有
close
方法,则最后一步将关闭弹出窗口

popup_window.close()
所以其他两个条件必须是真实的,才能达到这个目的,必须有一个窗口,它必须有一个
close
方法,然后调用
close
方法,窗口关闭

然后是逗号。从

逗号运算符计算其每个操作数(从左到右) 并返回最后一个操作数的值

我们有

("function" == typeof popup_window.close && popup_window.close(), popup_window = is_null);
让我们写得有点不同

(                          // ↓ 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
这里的技巧是,始终执行逗号之后的最后一部分,因为所有由逗号分隔的操作数都将被计算

这意味着,如果
弹出窗口
不是
为空
弹出窗口
显式设置为
为空
,而不管第二个条件如何

第二个条件也仅在
弹出窗口
为空
时执行,然后检查是否存在
关闭()
方法,并关闭窗口,逗号后的语句与该条件的结果无关

写得更简单(按照IMO应该写的方式)


这只是一种聪明(太聪明)的使用和检查大量语句的方法,如果它们都是真的,那么它将以逗号作为运算符,如果
window.close()
返回真的(如果存在弹出窗口,则返回弹出窗口),变量
popup\u window
设置为
为null
,这可能是基于该语句的
null
。确切地说,这就像
如果(truthy&&falsy&&truthy)
,最后一个从未被检查,因为第二个是falsy,它会在那里失败。这是一个编译的、缩小的脚本。当你写的时候:如果(a()&&b()&&c()){}a()将被测试,如果a()返回true,b()将被测试,如果b()返回true,c()将在你写的时候被测试:如果(a()&&b(),c()){}a()将被测试,如果a()返回true,b()将被测试,c()将被测试我知道有人写过这样的代码,所以这不需要由minifier生成;)回答得很好。谢谢
if ( popup_window != is_null ) {

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

    popup_window = is_null;

}