File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55## 执行上下文
66
7- 每个执行上下文又会分为内存创建( Creation Phase ) 与代码执行( Code Execution Phase ) 两个步骤,在创建步骤中会进行变量对象的创建(Variable Object )、作用域链的创建以及设置当前上下文中的 ` this ` 对象。所谓的 Variable Object ,又称为 Activation Object,包含了当前执行上下文中的所有变量、函数以及具体分支中的定义。当某个函数被执行时,解释器会先扫描所有的函数参数、变量以及其他声明:
7+ 每个执行上下文又会分为内存创建( Creation Phase) 与代码执行( Code Execution Phase) 两个步骤,在创建步骤中会进行变量对象的创建(Variable Object )、作用域链的创建以及设置当前上下文中的 ` this ` 对象。所谓的 Variable Object ,又称为 Activation Object,包含了当前执行上下文中的所有变量、函数以及具体分支中的定义。当某个函数被执行时,解释器会先扫描所有的函数参数、变量以及其他声明:
88
99```
1010'variableObject': {
6464
6565上面的语句将会报错,提示 ` ReferenceError: b is not defined ` ,即变量 b 未声明,这是因为 b 不是用 const 命令声明的,JavaScript 引擎不会将其提升,而只是视为对顶层对象的 b 属性的赋值。ES6 引入了块级作用域,块级作用域中使用 ` let ` 声明的变量同样会被提升,只不过不允许在实际声明语句前使用:
6666
67- ```
67+ ``` js
6868> let x = x;
6969ReferenceError : x is not defined
7070 at repl: 1 : 9
@@ -109,23 +109,21 @@ function b() {
109109
110110如果我们在作用域中重复地声明同名函数,则会由后者覆盖前者:
111111
112- ```
112+ ``` js
113113sayHello ();
114114
115+ function sayHello () {
116+ function hello () {
117+ console .log (' Hello!' );
118+ }
115119
116- function sayHello () {
117- function hello () {
118- console.log('Hello!');
119- }
120-
121- hello();
120+ hello ();
122121
123- function hello () {
124- console.log('Hey!');
125- }
122+ function hello () {
123+ console .log (' Hey!' );
124+ }
126125}
127126
128-
129127// Hey!
130128```
131129
@@ -145,50 +143,49 @@ sayHello();
145143
146144函数表达式遵循变量提升的规则,函数体并不会被提升至作用域头部:
147145
148- ```
146+ ``` js
149147sayHello ();
150148
149+ function sayHello () {
150+ function hello () {
151+ console .log (' Hello!' );
152+ }
151153
152- function sayHello () {
153- function hello () {
154- console.log('Hello!');
155- }
156-
157- hello();
154+ hello ();
158155
159- const hello = function () {
160- console.log('Hey!');
161- }
156+ const hello = function () {
157+ console .log (' Hey!' );
158+ };
162159}
163160
164-
165161// Hello!
166162```
167163
168164在 ES5 中,是不允许在块级作用域中创建函数的;而 ES6 中允许在块级作用域中创建函数,块级作用域中创建的函数同样会被提升至当前块级作用域头部与函数作用域头部。不同的是函数体并不会再被提升至函数作用域头部,而仅会被提升到块级作用域头部:
169165
170- ```
166+ ``` js
171167f; // Uncaught ReferenceError: f is not defined
172- (function () {
173- f; // undefined
174- x; // Uncaught ReferenceError: x is not defined
175- if (true) {
176- f();
177- let x;
178- function f() { console.log('I am function!'); }
179- }
180-
181- }());
168+ (function () {
169+ f; // undefined
170+ x; // Uncaught ReferenceError: x is not defined
171+ if (true ) {
172+ f ();
173+ let x;
174+ function f () {
175+ console .log (' I am function!' );
176+ }
177+ }
178+ })();
182179```
183180
184181# 避免全局变量
185182
186183在计算机编程中,全局变量指的是在所有作用域中都能访问的变量。全局变量是一种不好的实践,因为它会导致一些问题,比如一个已经存在的方法和全局变量的覆盖,当我们不知道变量在哪里被定义的时候,代码就变得很难理解和维护了。在 ES6 中可以利用 ` let ` 关键字来声明本地变量,好的 JavaScript 代码就是没有定义全局变量的。在 JavaScript 中,我们有时候会无意间创建出全局变量,即如果我们在使用某个变量之前忘了进行声明操作,那么该变量会被自动认为是全局变量,譬如 :
187184
188- ```
189- function sayHello(){
190- hello = " Hello World" ;
191- return hello;
185+ ``` js
186+ function sayHello () {
187+ hello = ' Hello World' ;
188+ return hello;
192189}
193190sayHello ();
194191
Original file line number Diff line number Diff line change 22
33## Babel
44
5- ```
5+ ``` js
66class Parent {
7-
87 constructor () {
98 this .a = 1 ;
109 }
11-
1210}
1311
14- class Child extends Parent {
15-
16- }
12+ class Child extends Parent {}
1713```
1814
1915``` js
You can’t perform that action at this time.
0 commit comments