gpt4 book ai didi

javascript - 来自未定义检查的引用错误

转载 作者:行者123 更新时间:2023-11-29 11:00:00 24 4
gpt4 key购买 nike

<分区>

我在未定义的检查中得到了令人困惑的结果。

在我的内存中,根据多个答案( 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>

我错过了什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com