关于一道面试提的解析

很久以前看到过一道这样的面试题:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <script>
 9         var foo = 1;
10         function bar() {
11             foo = 10;
12              return;
13              function foo() {}
14         }
15         bar();
16         console.log(foo);//1
17     </script>
18 </body>
19 </html>

以前不明白为什么结果会是1,这道题考的其实是对作用域的理解和预编译的理解,第13行的函数声明会提升到bar()函数最前面,所以bar函数里面的foo=10,只是作用在局部作用域,不会影响去全局变量,现在有个疑问  function申请与var声明 哪个权重比较高,一般是function ,但也看情况,下一篇讲解。

坚持下去就能成功
原文地址:https://www.cnblogs.com/suoking/p/4929074.html