为什么变量声明总是覆盖函数声明? [英] Why can variable declarations always overwrite function declarations?
本文介绍了为什么变量声明总是覆盖函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var a = 1;
函数a(){};
typeof a //数字
或者如果我在变量
function a(){};
var a = 1;
typeof a //数字
最后 typeof
result总是 number
我发现了关于执行上下文
在 http://davidshariff.com / blog / what-is-the-execution-context-in-javascript /
在执行功能代码,创建执行上下文。
......
扫描变量声明的上下文:
如果变量对象中已经存在变量名,则不执行任何操作并继续扫描。
但这似乎不起作用。
那么我该如何解释它? 解决方案
这是关于JavaScript的变量提升。试试这个:
var a = 1;
var a = function(){};
typeof a //函数
No matter whether I define the function after the variable
var a = 1;
function a() {};
typeof a // number
Or if I define the function before the variable
function a() {};
var a = 1;
typeof a // number
the final typeof
result is always number
I found some explanation about execution context
in http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/
Before executing the function code, create the execution context.
......
Scan the context for variable declarations:
If the variable name already exists in the variable object, do nothing and continue scanning.
but this does not seem to work.
So how can I explain it?
解决方案
It's to do with JavaScript's variable hoisting. Try this instead:
var a = 1;
var a = function() {};
typeof a // function
这篇关于为什么变量声明总是覆盖函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文