【问题标题】:JavaScript variable hoisting explanationJavaScript 变量提升解释
【发布时间】:2015-09-18 06:35:18
【问题描述】:

我看到了以下关于 javascript 中的变量提升的文章。文章总结了以下三点。

1. All declarations, both functions and variables, are hoisted to the top of the containing scope, before any part of your code is executed.
2. Functions are hoisted first, and then variables.
3. Function declarations have priority over variable declarations, but not over variable assignments.

Site Point

var showState = function() {
  console.log("Idle");
};

function showState() {
  console.log("Ready");
} 

showState(); 

我知道代码被javascript引擎解释为

function showState() { // moved to the top (function declaration)
    console.log("Ready");
}

var showState; // moved to the top (variable declaration)
showState = function() { // left in place (variable assignment)
    console.log("Idle");
};

showState();

但是,我无法理解摘要中第三点的含义。谁能解释第三点?第三点是什么意思?

根据第三点的解释,下面的sn -p应该返回8,函数bar()。但它说未定义,函数 bar()。

console.log(foo);
console.log(bar);
var foo = 8;
function bar() {
    console.log("bar");
}

【问题讨论】:

  • 在您的情况下,第 3 点意味着变量赋值语句 var foo 将在您的 function bar() 被提升之前被提升。所以实际上你的解释代码是var foo; function bar(),如果你没有赋值语句,它会是function bar(); var foo
  • foo = 8;不是变量赋值,var foo;是变量声明部分吗?
  • 你能解释一下为什么会这样吗?我认为函数具有优先权是有含义的,因为它们可能会被使用。
  • @AdityaParab 这是否意味着它也适用于函数表达式?因为它也有任务。
  • 我不知道为什么这个规则是由 JS 引擎实现的。我只是根据我开发 js 代码的经验做出一个疯狂的猜测。在一个易于维护的代码中,鼓励开发人员向外界公开方法——而不是变量。而且由于这是我们公开的方法,我们不能在外面得到undefined。 :) 我真诚地相信这就是为什么函数优先于未初始化的变量的原因。

标签: javascript hoisting


【解决方案1】:

来自您链接到的文章:

在上面的代码中,我们看到函数声明采用 优先于变量声明。在下一个例子中 我们会看到,当我们有函数声明与变量时 分配,最后一个优先。

var showState = function() {
  console.log("Idle");
};

function showState() {
  console.log("Ready");
} 

showState();            // output: Idle

函数声明做了两件事:

  1. 它声明了一个与函数同名的变量
  2. 它将函数分配给该变量

这两个都被提升了,而不仅仅是变量声明。 (这与带有关联赋值的 var 语句不同,其中只有声明被提升)。

这意味着尽管= function() { 代码在前,但later 函数声明仍然首先运行,因此= function() { 可以覆盖它。

【讨论】:

  • @Quentin var showState; showState = function() { console.log("ready"); }; showState = function() { console.log("空闲"); };显示状态();这是内部发生的事情吗?
  • @BRS — 因为函数表达式和函数声明之间还有其他区别:并非如此。
  • @Quentin 下面对函数表达式的理解是否正确?变量显示状态;功能 showState() { console.log("Ready"); } showState = function() { console.log("空闲"); };显示状态();对于变量赋值,以下 var foo;功能 bar() { console.log("bar"); } 控制台.log(foo);控制台.log(条); foo = 8;
  • @BRS 不,正确的解释是function showState(){console.log("Ready");}; var showState; showState = function(){console.log("Idle");}; showState();
  • @BRS 在这种情况下,变量showState 被该赋值覆盖。
【解决方案2】:

你的最后一个例子被解释为

function bar() {
    console.log("bar");
}    
var foo;

console.log(foo); // here 'foo' is undefined
console.log(bar); // bar is of type function
foo = 8;

【讨论】:

  • 应该是下面的吧?由于分配优先?变富;功能 bar() { console.log("bar"); } 控制台.log(foo);控制台.log(栏); foo = 8;
  • 不,函数bar被声明首先然后foo被声明。
  • :) 那么文章摘要中的第三句话有什么意义呢?是不是错了?
  • 想查看 Aditya 对问题的第一条评论吗?谢谢。
  • @BRS 不,这是正确的。用户Quentin 解释了原因。
【解决方案3】:

基本上 - 假设我们有,没有特定的顺序

function hello(){};
var sup;
var yo = 4;

通过变量提升,订单将变为

    var yo = 4;
    function hello(){};
    var sup;

因为 var 赋值优先于函数,函数优先于函数声明。

【讨论】:

  • console.log(typeof hello); console.log(你的类型); console.log(typeof sup);函数你好() {} var yo = 4; var sup;产生函数,未定义,未定义。但根据你的说法,第二个应该是一个数字吧?
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 2015-01-01
  • 2019-05-07
  • 2021-12-23
  • 2017-07-18
相关资源
最近更新 更多