<分区>
我在未定义的检查中得到了令人困惑的结果。
在我的内存中,根据多个答案( 1 2 3 4 5 ),以下代码应该可以工作。
// bar is not defined
if (bar) console.log("should not execute");
if (!bar) console.log("should execute");
var foo = bar || 'foo'; // should assign 'foo' but is undefined
但是在 Chrome(版本 63.0.3239)和 Firefox Nightly(版本 60.0a1)上我得到了一个
未捕获的 ReferenceError:未定义栏
这发生在没有严格模式的控制台和链接脚本上
// linked-script.js
(function() {
if (bar) console.log("should not execute");
if (!bar) console.log("should execute");
var foo = bar || 'foo';
})();
// index.html
<script type="text/javascript" src="linked-script.js"></script>
我错过了什么?