Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 编译时javascipt函数和变量的差异_Javascript_Function_Var - Fatal编程技术网

Javascript 编译时javascipt函数和变量的差异

Javascript 编译时javascipt函数和变量的差异,javascript,function,var,Javascript,Function,Var,哪个输出是 var a; if (true) { a = {a: 2}; function a() {} a.ot = '9'; a = {a: 4}; console.log('1', a); } console.log('2', a); 谁能告诉我为什么?非常感谢@ASDFGerte指出的,解释了答案,但让我做一些从你的例子到答案中给出的例子之间的桥梁工作 首先,让我们删除if(true):只有存在块才相关。其次,让我们用普通字符串替换{a:…}。行a.ot='9'也不相

哪个输出是

var a; 

if (true) {
 a = {a: 2};
 function a() {}
 a.ot = '9'; 
 a = {a: 4};
 console.log('1', a);
}

console.log('2', a);
谁能告诉我为什么?非常感谢@ASDFGerte指出的,解释了答案,但让我做一些从你的例子到答案中给出的例子之间的桥梁工作

首先,让我们删除
if(true)
:只有存在块才相关。其次,让我们用普通字符串替换
{a:…}
。行
a.ot='9'
也不相关,所以让我们删除它。因此,现在我们将讨论以下问题:

1 {a: 4}
2 {a: 2, ot: "9"}
结果如下:

var a; 

{
 a = 'before func declaration';
 function a() {} 
 a = 'after func declaration';
 console.log('Inside block:', a);
}

console.log('Outside block:', a);
Inside block: after func declaration
Outside block: before func declaration
在这一点上,应该更清楚如何应用。本质上,代码的编写方式如下:

var a; 

{
 a = 'before func declaration';
 function a() {} 
 a = 'after func declaration';
 console.log('Inside block:', a);
}

console.log('Outside block:', a);
Inside block: after func declaration
Outside block: before func declaration

编译?安威,你期望的结果是什么?这能回答你的问题吗?长话短说:这是634534个黑魔法陷阱之一,当你不“严格使用”时就会发生;而是
b='在func声明之前';a=b