Ref: 深入理解js的变量提升和函数提升

一、变量提升

简直就是es5的遗毒!

console.log(global); // undefined 竟然能打印?因为变量提升,下一行就有定义
var global = 'global';
console.log(global); // global function fn () {
  console.log(a); // undefined  竟然能打印?因为变量提升,下一行就有定义
  var a = 'aaa';
  console.log(a); // aaa
}
fn();

实际运行的代码过程,也就是编译器自己做了些手脚。

var global; // 变量提升,全局作用域范围内,此时只是声明,并没有赋值
console.log(global); // undefined
global = 'global'; // 此时才赋值
console.log(global); // 打印出global function fn () {
  var a; // 变量提升,函数作用域范围内
  console.log(a);
  a = 'aaa';
  console.log(a);
}
fn();

二、函数提升

js中创建函数有两种方式:函数声明式函数字面量式。只有函数声明(第一种)才存在函数提升!

console.log(f1); // function f1() {}
console.log(f2); // undefined
function f1() {}  // 这种传统意义上的,就可以
var f2 = function() {}

实际运行的代码过程,也就是编译器自己做了些手脚。

function f1() {} // 函数提升,整个代码块提升到文件的最开始,函数提升起来了,嘿嘿
console.log(f1);
console.log(f2);
var f2 = function() {}

[JS] Topic - variable and function hoisting的更多相关文章

  1. [JS] Topic - why "strict mode" here

    Ref: Javascript 严格模式详解 使得Javascript在更严格的条件下运行: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全 ...

  2. Variable hoisting Function hoisting

    Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a var ...

  3. js regex variable & Set, Map

    js regex variable & Set, Map regex, variable, Set, Map, 交集, 差集, 并集, https://stackoverflow.com/qu ...

  4. [Node.js] 05 - Modules and Function

    一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...

  5. 【微信小程序】在js中导入第三方js或自己写的js,使用外部js中的function的两种方法 import和require的区别使用方法 【外加:使用第三方js导出的默认function的调用方法】

    如下 定义了一个外部js文件,其中有一个function import lunaCommon from '../lunaCommon.js'; var ctx = wx.getStorageSync( ...

  6. learning scala How To Create Variable Argument Function - varargs :_ *

    Scala collection such as List or Sequence or even an Array to variable argument function using the s ...

  7. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  8. js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit

    js in depth: Object & Function & prototype & proto & constructor & classes inher ...

  9. js in depth: closure function & curly function

    js in depth: closure function & curly function 闭包, 科里化 new js 构造函数 实例化, 不需要 new var num = new Ar ...

随机推荐

  1. git 变基(无卵用)

    在当前分支执行rebase即可,会将提交的记录变成一条直线 git rebase

  2. java计算今天是今年的第几天

    Calendar.getInstance().get(Calendar.DAY_OF_YEAR)

  3. asp.net c#并行调用service层代码

    public ActionResult Home(AdviserSearchModel model) { //顾问列表需要的当前城市的下级地区 var ip = "117.82.196.19 ...

  4. POI设置excle单元格样式

    Java利用POI生成Excel强制换行 使用POI创建一个简单的   myXls.xls   文件       常用的包为   org.apache.poi.hssf.usermodel.*;    ...

  5. [前端] 记录工作中遇到的各种问题(Bug,总结,记录)

    最近一年,在开发实践过程中遇到了不少问题,大多都能得到解决 部分知其原理,部分只能做到解决问题,而半年前遇到的问题,或多或少都忘得差不多了 是该记录一下一些问题,防止再遇到就得再查资料了 1. 浏览器 ...

  6. Spring MVC 中使用 Google kaptcha 验证码

    验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...

  7. 不依赖三方库从图像数据中获取宽高-gif、bmp、png、jepg

    int extract_pic_info(const BYTE *pic, const uint32_t size, int &width, int &height) { ; widt ...

  8. oracle 11g 安装及netca,dbca乱码之解决

    在中文Linux下安装Oracle 11g,运行runInstaller后默认会出现乱码,解决办法如下: 1.准备字体zysong.ttf,点击下载,解压下载到的fallback 2.使用归档管理器打 ...

  9. SpringCloud Stream生产者配置RabbitMq的动态路由键

    在写这个文章前不得不吐槽目前国内一些blog的文章,尽是些复制粘贴的文章,提到点上但没任何的深入和例子.......... 经过测试下来总结一下RabbitMQ的Exchange的特性: 1.dire ...

  10. 安装 scws出现 autoconf 需要先安装

    安装在终端操作, curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-latest.tar.gz tar xzf autoconf-latest.t ...