You Don't Know JS: Scope & Closures (第4章: Hoisting)
Chapter4: Hoisting
变量附加到哪个层次的scope,由它们在哪里和如何声明(let, var)来决定。
Function scope/Block scope都有相同的法则:任何变量在一个scope内声明,则这个变量附加到这个作用域上。
但有一个细节问题:当声明declarations出现在一个作用域中的不同的位置的时候,scope附加如何与declarations协作?
Chicken or The Egg?
temptation: a strong desire to have or do sth even thought you know you should not(邪念,诱惑人的事物)
当程序执行时,JS 代码被一行行,从上到下的顺序被解译。但有例外:
a = 2; var a; console.log( a ); 输出2
可是:
console.log( a ); var a = 2;
//输出undefined
怎么回事? 一个先有鸡还是先有蛋的问题。 the declaration是蛋, assignment是鸡。
The Compiler Strikes Again 编译器又罢工了
Engine实际是先编译JS code,在interprtes it之前。
编译器先声明变量,然后Engine在Scope中查询这个变量,如果发现就分配它。

所以思考事情的最好的方式是:
所有的声明declarations,包括变量和函数,被首先处理processed。在你的代码被执行executed前。
var a = 2; 其实是2个statements: var a;和a = 2;
var a 是声明,在编译阶段被处理;
a = 2是assignment, 会留在执行阶段execution phase处理。
所以,前2个例子就可以理解了:
//第一个例子:
var a;
a = 2;
console.log(a); // 2 //第二个例子:
var a;
console.log(a); //undefined
a = 2;
结论:先有蛋(declarations),后有
You Don't Know JS: Scope & Closures (第4章: Hoisting)的更多相关文章
- You Don't Know JS: Scope & Closures (第3章: 函数 vs 块作用域)
第二章,作用域由一系列的bubbles组成.每一个都代表了一个container或bucket,装着被声明的identifiers(variables, functions).这些bubbles相互嵌 ...
- You Don't Know JS: Scope & Closures (第2章: Lexical Scope)
2种主要的models for how scope work. 最普遍的是Lexical Scope. 另一种 Dynamic Scope.(在Appendix a中介绍.和Lexical Scope ...
- (未完成👃)You Don't Know JS: Scope & Closures (第5章: Scope & Closures)
Chapter 5: Scope Closure 我们到达这里时,已经对作用域如何工作有了非常健康稳固的理解. 下面,我们转移注意力到一个及其重要,但长期难以理解,几乎是神话中的部分语言:Closur ...
- You Don't Know JS: Scope & Closures (第一章:什么是Scope)
Content What is Scope? Lexical Scope Function Vs. Block Scope Hoisting Scope Closures Appendix: Dyna ...
- You Don't Know JS: Scope & Closures(翻译)
Chapter 1: What is Scope? 第一章:什么是作用域 One of the most fundamental paradigms of nearly all programming ...
- You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)
JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...
- js的closures(闭包)
JS中的闭包(closure) 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面就是我的学习笔记,对于Javascript初学者应该是很有用 ...
- Angular JS Scope(作用域)
Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Scope 可应用在视图和控制器上. 当你在 Ang ...
- You Don't Know JS: Async & Performance(第一章, 异步:now & later)
Chapter 1: Asynchrony: Now & Later 在一门语言中,比如JavaScript, 最重要但仍然常常被误解的编程部分是如何在一个完整的时间周期表示和操作程序行为. ...
随机推荐
- python --- 05 字典 集合
一.字典 可变数据类型 {key:value}形式 查找效率高 key值必须是不可变的数据类型 1.增删改查 1).增 dic["新key"] = "新va ...
- String的getBytes()方法 以及 new String()
在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这表示在不同的操作系统下,返回的东西不一样! String.getBytes(Stringdecode) ...
- Linux - PWM的驱动编写【转】
本文转载自:https://blog.csdn.net/u012264124/article/details/77482853 比如要用到pwm1,那么首先要保证这个pwm1并没有被别的驱动程序占用. ...
- (转)Spring Boot(二) & lombok
(二期)5.springboot框架集成与lombok [课程五]springb...mbok.xmind0.1MB [课程五预习]spr...mbok.xmind0.1MB springboot的版 ...
- How Flyway works
The easiest scenario is when you point Flyway to an empty database. It will try to locate its schema ...
- 自己网盘的页面生成器(私用公开-Golang)
虽说我的网盘(exm,也许页面确实丑了点,不过页面生成的样式你自己可以改)美工已经被乱刀砍死,但是还是有小伙伴问我是怎么搭建的 关于搭建 这个真没什么好说的,vps我只安装了nginx,然后配置域名指 ...
- 深度学习课程笔记(十六)Recursive Neural Network
深度学习课程笔记(十六)Recursive Neural Network 2018-08-07 22:47:14 This video tutorial is adopted from: Youtu ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- C语言 分割字符串
对指针的操作有点迷糊 只好采用下面一些比较low的手段 char str[100]; char delims[] = ";"; char *result = NULL; sprin ...
- 【Selenium2】【Jenkins】
1. 下载Tomcat ,Windows7 环境,http://tomcat.apache.org/ 我下载的是版本8 2. 下载Jenkins,Windows7 环境,http://jenkins ...