JS函数和变量名称冲突
在JS中如果函数名与变量名冲突,JS是怎么执行的?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <script> 2 console.log(sum);//function sum(){} 3 4 function sum(){} 5 var sum = 10; 6 7 console.log(sum);//10 8 </script>
JS会先把变量的声明放在最前面,然后将定义式函数放在变量声明的后面,后面赋值语句按原来的顺序I依次执行.
因此上面的代码等价于
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <script> 2 var sum; 3 function sum(){} 4 5 console.log(sum); 6 sum = 10; 7 console.log(sum); 8 </script>