在JavaScript中提升变量 [英] Hoisting variables in JavaScript

查看:73
本文介绍了在JavaScript中提升变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解变量的提升是在Java Script中完成的。我无法理解为什么输出为未定义

I understand Hoisting of variables is done in Java Script. I am unable to get why it outputs as undefined

 do_something()
    {
    var foo = 2;    
    console.log(foo);   
    } do_something()  // it prints 2

 do_something()
     {        
     console.log(foo);  var foo = 2;  
     } do_something()  // it prints undefined

因为javascript也提升第二个函数应该按照我的理解打印2。购买为什么不呢

As javascript do hoisting the second function also should print 2 as per my understand.buy why doesn't it

推荐答案

只有声明被悬挂。分配的变量不会被提升。所以你

Only the declaration is hoisted. the assigned variables are not hoisted. So you are

这篇关于在JavaScript中提升变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆