gpt4 book ai didi

javascript - 两个令人困惑的 Javascript 测验

转载 作者:行者123 更新时间:2023-11-28 11:32:35 25 4
gpt4 key购买 nike

只是对一些测验感到困惑。

测验一

var x = 1;
if (function f(){}) {
x += typeof f;
}
alert(x);

答案:

x 的答案是“1undefined”

困惑:

if 条件下的“function f(){}”究竟发生了什么?

测验二

(function(x){
delete x;
return x;
})(1);

答案:

输出为 1。

困惑:

为什么删除不起作用?什么时候“删除”有效,什么时候无效?

最佳答案

由于任何函数都是 true,您只需将数字 1 与字符串 'undefined' 连接起来。在第二个中,delete仅适用于对象属性。

关于javascript - 两个令人困惑的 Javascript 测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731632/

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