[JS] Topic - variable and function hoisting
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的更多相关文章
- [JS] Topic - why "strict mode" here
Ref: Javascript 严格模式详解 使得Javascript在更严格的条件下运行: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全 ...
- Variable hoisting Function hoisting
Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a var ...
- js regex variable & Set, Map
js regex variable & Set, Map regex, variable, Set, Map, 交集, 差集, 并集, https://stackoverflow.com/qu ...
- [Node.js] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- 【微信小程序】在js中导入第三方js或自己写的js,使用外部js中的function的两种方法 import和require的区别使用方法 【外加:使用第三方js导出的默认function的调用方法】
如下 定义了一个外部js文件,其中有一个function import lunaCommon from '../lunaCommon.js'; var ctx = wx.getStorageSync( ...
- 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 ...
- js in depth: arrow function & prototype & this & constructor
js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...
- js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit
js in depth: Object & Function & prototype & proto & constructor & classes inher ...
- js in depth: closure function & curly function
js in depth: closure function & curly function 闭包, 科里化 new js 构造函数 实例化, 不需要 new var num = new Ar ...
随机推荐
- Saltstack cp.get 模块
语法 salt '*' cp.get_file salt://rr /etc/rr cp.get_url 可以从一个URL地址下载文件,URL可以是msater上的路径(salt://),也可以是h ...
- 控制台获取AngularJS某个元素的Scope
如何在控制台获取到某个元素的Scope呢? 假设,页面元素为: <label>Name:</label><input type="text" ng-m ...
- tmux分屏幕
1. tmux a -t fly 连接上tmux 2. 左右分屏幕,ctrl+a ,再按% 上下分屏: ctrl+a, 再按“ 切换屏幕: ctrl+a, 再按o 关闭终端: ctrl+a, 再 ...
- Spark:几种给Dataset增加列的方式、Dataset删除列、Dataset替换null列
几种给Dataset增加列的方式 首先创建一个DF对象: scala> spark.version res0: String = .cloudera1 scala> val , , 2.0 ...
- 从入门到精通Puppet的实践之路
本文有感于<精通Puppet配置管理工具>在豆瓣上的某些差评而顺手写的书评. 半路出家 故事要从12年初说起. 某天,部门老大让我所在team的老大调研一下当下业界的配置管理工具.于 ...
- packageOfficialDebug和resourceFile does not exist.
Android Studio运行时候报packageOfficialDebug错误 报错信息为 Error:A problem was found with the configuration of ...
- MDX Cookbook 07 - 在不同层次结构的成员中实现 逻辑 OR 的效果
第一个示例:查看所有包括黑色产品的子目录产品中的 Reseller Order Quantity 和 Reseller Order Count. 第二个示例:和第一个示例查询结构一样,只是筛选的是大小 ...
- JVM的7种垃圾收集器:主要特点 应用场景 设置参数 基本运行原理
原文地址:https://blog.csdn.net/tjiyu/article/details/53983650 下面先来了解HotSpot虚拟机中的7种垃圾收集器:Serial.ParNew.Pa ...
- Lintcode 730 所有子集的和
已知: 给一整数 n, 我们需要求前n个自然数形成的集合的所有可能子集中所有元素的和. 示例: 给出 n = , 返回 可能的子集为 {{}, {}, {, }}. 子集的元素和为 + + + = 给 ...
- CMD递归文件夹
SET dir=%~dp0 SET /a cnt=0 echo dir is: %dir% cd /d %dir% for /R %dir% %%i in (*.apk) do ( set /a cn ...