gpt4 book ai didi

javascript - 将变量名赋给相同的变量名

转载 作者:可可西里 更新时间:2023-11-01 02:05:31 26 4
gpt4 key购买 nike

执行时

var a=b=c=d=e=f=a; 
//no error(a has not initialize before)

var x=y;
//ReferenceError: y is not defined

第一个代码如何才能像 a 之前已经初始化一样执行。

最佳答案

是因为变量提升。 var x = EXPR; 实际上转换为:

// beginning of the block (function/file)
var x; // === undefined
// ...
// the actual position of the statement
x = EXPR

对于您的示例,这意味着:

var a;  // === undefined
a = b = c = d = e = f = a;

请注意,只有 a 是使用 var 声明的 - 因此您正在创建大量全局变量,这总是一件坏事!

关于javascript - 将变量名赋给相同的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520765/

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