[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 ...
随机推荐
- Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' from incompatible type 'InfchangeVC *const __strong'
出现 Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from inco ...
- 【译】如何在 Android 5.0 上获取 SD卡 的读写权限
因为最近项目需要,涉及到 SD卡 的读写操作,然而申请 <!-- 读写权限 --> <uses-permission android:name="android.permi ...
- c# pictureBox1.Image的获得图片路径的三种方法 winform
代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...
- 总结·展望
学了算法也有半年了.也是学期末,确实是该总结了.半年来说不上多努力,毕竟不如高中那时候早晨5点起晚上12点睡,但也确实学到不少东西(尽管眼下来说根本用不到并且我也不确定以为会不会去用.毕竟专业放在那里 ...
- AngularJS中的transclusion案例
AngularJS中的transclusion类似于包含关系. 通常,这样定义一个directive: <mydirective someprop=""></my ...
- python测试开发django-46.xadmin添加action动作
前言 Action插件在数据列表页面上提供数据选择功能.可以在Action之后专门处理所选数据.批量删除功能作为默认操作提供. action文档 要启用Action,开发人员可以设置Model Opt ...
- nssm和AlwaysUp来包装exe文件为windows服务
最近遇到要把windows exe文件部署为service,因为原先开发为exe程序,现在有不想修改code改为service,但是部署必须是service服务, 所以我们需要一个包装器来包装exe为 ...
- 为什么和什么是 DevOps?
原文地址 本文内容 为什么 DevOps 什么是 DevOps DevOps 所带来的好处 如何将 DevOps 落到实处? 关于 DevOps 的澄清 参考资料 编写软件之所以难,是因为没有哪两个软 ...
- 使用ThreadLocal来实现一个本地缓存
大家应该知道,用户从发起请求,到服务器响应的这个过程中,在服务器中是在一个线程中的.如果我们吧查询出来的对象放到这个线程自己的缓存中,到用户请求结束时,把这些东西清理掉,应该是一个不错的cache方案 ...
- sql随机查询数据语句(NewID(),Rnd,Rand(),random())
SQL Server: 代码如下 复制代码 Select TOP N * From TABLE Order By NewID() NewID()函数将创建一个 uniqueidentifier 类型的 ...