javascript – 为什么变量声明总是会覆盖函数声明?

无论我是否在变量后定义函数
var a = 1;
function a() {};
typeof a // number

或者如果我在变量之前定义函数

function a() {};
var a = 1;
typeof a // number

最终的结果类型总是数字

我在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.

但这似乎不起作用.

那我怎么解释呢?

解决方法

它与JavaScript的 variable hoisting有关.请尝试这样做:
var a = 1;
var a = function() {};
typeof a // function

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小