JavaScript面试题:提升(hoisting)是什么意思?

2018-11-0108:50:50WEB前端开发Comments2,339 views字数 324阅读模式

话题: JavaScript 难度: ⭐⭐⭐⭐文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7609.html

提升(hoisting)是指JavaScript的解释器将所有的变量和函数声明都提升到该作用域的顶部,有两种提升类型:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7609.html

  • 变量提升
  • 函数提升

在一个作用域中通过声明的变量和函数在整个作用域中都可以使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7609.html

var a = 2;
foo();                 // works because `foo()`
                         // declaration is "hoisted"

function foo() {
    a = 3;
    console.log( a );   // 3
    var a;             // declaration is "hoisted"
                         // to the top of `foo()`
}

console.log( a );   // 2
复制代码

虽然foo()函数在后面定义,但是在前面也可以调用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7609.html

作者:Fundebug
来源:掘金文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/7609.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/7609.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定