javascript if 语句有这种用法?

来源:百度知道 编辑:UC知道 时间:2024/05/05 03:02:40
if(window.opener){
window.opener.location.reload(true);
window.close();
}

window.opener返回父窗口对象,不是boolean类型的啊。怎么直接做了if条件了?请解释一下这什么意思?

弱类型语言么,你是学JAVA要么C#的吧,这种写法在以前的语言里很常见。
这里如果返回的对象存在就为true,如果是空指针就是false。

如果这样写你就明白了?

if (window.opener != null)

通常我们判断一个对象都是这样的

if (document.getElementById('aa')) //对象存在,进入if

if(0||null|undefined){//这些都会返回false

}
弱类型的特点,这个也是判断变量是否存在的最佳方法