gpt4 book ai didi

javascript - 为什么变量声明总是可以覆盖函数声明?

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:31 26 4
gpt4 key购买 nike

不管我是不是在变量后面定义函数

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

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

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

决赛typeof结果总是 number

我找到了一些关于 execution context 的解释在 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

关于javascript - 为什么变量声明总是可以覆盖函数声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21139719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com