gpt4 book ai didi

Javascript 和提升

转载 作者:行者123 更新时间:2023-12-02 08:00:32 25 4
gpt4 key购买 nike

在我的代码中,x 值未定义。如果我删除 if block ,x 值显示为 77。我不明白为什么 if block 正在修改 x 值。

var x = 77;

function fn() {
if (false) {
var x = 55;
}
console.log(x);
}

fn();

最佳答案

var x = 77;

function fn() {
if (false) {
var x = 55;
}
console.log(x); // undefind
}

fn();

在中间阶段,x 将被提升到其作用域:

    var x = 77;
function fn() {
//var x = undefind; intermediate phase
if (false) {
var x = 55;
}
console.log(x);
}

fn();

var x = 77;

function fn() {
if (false) {
x = 55;
}
console.log(x); // 77
}

fn();

关于Javascript 和提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58132681/

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