ES6 & Javascript

es 和 js 的关系: 前者是后者的规格,后者是前者的实现
通过 es-checker 检查当前 node 环境对于 es6 的支持情况

1
2
npm install -g es-checker
es-checker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
λ es-checker

ECMAScript 6 Feature Detection (v1.4.2)

Variables
√ let and const
√ TDZ error for too-early access of let or const declarations
√ Redefinition of const declarations not allowed
√ destructuring assignments/declarations for arrays and objects
√ ... operator

Data Types
√ For...of loop
√ Map, Set, WeakMap, WeakSet
√ Symbol
√ Symbols cannot be implicitly coerced

Number
√ Octal (e.g. 0o1 ) and binary (e.g. 0b10 ) literal forms
√ Old octal literal invalid now (e.g. 01 )
√ Static functions added to Math (e.g. Math.hypot(), Math.acosh(), Math.imul() )
√ Static functions added to Number (Number.isNaN(), Number.isInteger() )

String
√ Methods added to String.prototype (String.prototype.includes(), String.prototype.repeat() )
√ Unicode code-point escape form in string literals (e.g. \u{20BB7} )
√ Unicode code-point escape form in identifier names (e.g. var \u{20BB7} = 42; )
√ Unicode code-point escape form in regular expressions (e.g. var regexp = /\u{20BB7}/u; )
√ y flag for sticky regular expressions (e.g. /b/y )
√ Template String Literals

Function
√ arrow function
√ default function parameter values
√ destructuring for function parameters
√ Inferences for function name property for anonymous functions
× Tail-call optimization for function calls and recursion

Array
√ Methods added to Array.prototype ([].fill(), [].find(), [].findIndex(), [].entries(), [].keys(), [].values() )
√ Static functions added to Array (Array.from(), Array.of() )
√ TypedArrays like Uint8Array, ArrayBuffer, Int8Array(), Int32Array(), Float64Array()
√ Some Array methods (e.g. Int8Array.prototype.slice(), Int8Array.prototype.join(), Int8Array.prototype.forEach() ) added to the TypedArray prototypes
√ Some Array statics (e.g. Uint32Array.from(), Uint32Array.of() ) added to the TypedArray constructors

Object
√ __proto__ in object literal definition sets [[Prototype]] link
√ Static functions added to Object (Object.getOwnPropertySymbols(), Object.assign() )
√ Object Literal Computed Property
√ Object Literal Property Shorthands
√ Proxies
√ Reflect

Generator and Promise
√ Generator function
√ Promises

Class
√ Class
√ super allowed in object methods
√ class ABC extends Array { .. }

Module
× Module export command
× Module import command


=========================================
Passes 39 feature Detections
Your runtime supports 92% of ECMAScript 6
=========================================

变量

es6 中我们可以通过 let 定义变量

不能重复声明

1
2
let str = "";
let str = "123";

变量重复定义:
Identifier ‘str’ has already been declared

暂时性死区

在变量声明之前调用了该变量,就会形成这个变量的暂时性死区。

1
2
console.log(str);
let str = "";

声明之前调的语法错误
Cannot access ‘str’ before initialization

变量提升

1
2
3
4
5
6
7
8
// 通过var 定义了一个变量str并在声明之前调用
console.log(str);
var str = "";

// 变量提升在内存中的执行时,会将str前置
var str;
console.log(str);
str = "";

块及作用域

作用域

  • 全局作用域
  • 块级作用域

常量

es6 中我们可以通过 const 定义常量

不能重复声明

暂时性死区

没有变量提升

块级作用域

恒定的值

通过 const 定义的变量的值(数据类型和引用)不可以重新赋值。

  • 数据类型不可改
  • 不可以对 const 的值进行重新赋值的操作
  • 可以修改数据的项,不可以修改数据的指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const a = 123;
a = 456;
// 常量的值不可以被修改
// const a = 123;
// a = 456

// 重新赋值
// const arr = [1, 23, , 5, , 6, 6];
// arr = [1, 23, , 5, , 6, 6];

// 引用相同,重新赋值不可
// const temp = [1, 23, , 5, , 6, 6];
// const arr = temp;
// arr = temp;

// 数组特殊情况
// const temp = [1, 23, , 5, , 6, 6];
// temp[3] = 123;
// temp.push(132)

// 对象的特殊情况
var temp = { name: "zhangsan" };
temp.name = "123";
temp.age = 123;
console.log(temp);
// const obj = temp;
// obj = temp;

错误类型

  • syntax error 语法错误
  • Reference Error 引用错误
  • type error 类型错误