ECMAScript 2015 Features
ES6 包含了以下这些新特性:
- Arrows 箭头函数
- Classes 类
- Enhanced Object Literals 增强的对象字面量
- Template Strings 模板字符串
- Destructuring 解构
- Default + Rest + Spread 默认参数+不定参数+参数展开
- Let + Const 操作符
- Iterators 迭代器 + For..Of 循环
- Generators 生成器
- Unicode 统一码
- Modules 模块
- Module Loaders 模块加载器
- Map + Set + Weakmap + Weakset 数据结构
- Proxies 代理
- Symbols 符号
- Subclassable Built-ins 可子类化内建对象
- Promises 对象
- Math + Number + String + Object APIs
- Binary and Octal Literals 二进制和八进制字面量
- Reflect 反射 API
- Tail Calls 尾调用
Arrows and Lexical This
Arrows are a function shorthand using the =>
syntax.
They are syntactically similar to the related feature in C#, Java 8 and CoffeeScript.
They support both expression and statement bodies.
Unlike functions, arrows share the same lexical this
as their surrounding code.
If an arrow is inside another function, it shares the “arguments” variable of its parent function.
expression body
Statement bodies
Lexical this
Lexical arguments
![](https://i-blog.csdnimg.cn/blog_migrate/51bb851b8dda39694672d34a4736314e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a9d1a64384302e966f48dcdcb2b8a17e.png)
Classes
ES2015 classes are a simple sugar over the prototype-based OO pattern.
Having a single convenient declarative form makes class patterns easier to use, and encourages interoperability.
Classes support prototype-based inheritance, super calls, instance and static methods and constructors.
![](https://i-blog.csdnimg.cn/blog_migrate/af2c1a8b2d5e426c0347200be2d6d0a2.png)
Enhanced Object Literals
Object literals are extended to support setting the prototype at construction, shorthand for foo: foo
assignments, defining methods and making super calls.
Together, these also bring object literals and class declarations closer together, and let object-based design benefit from some of the same conveniences.
The
__proto__
property requires native support, and was deprecated in previous ECMAScript versions.Most engines now support the property, but some do not.
Also, note that only web browsers are required to implement it, as it's in Annex B.
It is available in Node.
![](https://i-blog.csdnimg.cn/blog_migrate/70e1879ec6a8c053e89e8fd46954ef53.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6d9f3b75c77ae1ff9f312f8957303719.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c7a7cae21ddf83f12d4b2d77b2d873f5.png)
Template Strings ( `键盘左上角的波浪号键` )
Template strings provide syntactic sugar for constructing strings.
This is similar to string interpolation features in Perl, Python and more.
Optionally, a tag can be added to allow the string construction to be customized, avoiding injection attacks or constructing higher level data structures from string contents.
Destructuring 解构
Destructuring allows binding using pattern matching, with support for matching arrays and objects.
Destructuring is fail-soft, similar to standard object lookup foo["bar"]
, producing undefined
values when not found.
object matching
object matching shorthand
parameter position
Default + Rest + Spread
Callee-evaluated default parameter values.
Turn an array into consecutive arguments in a function call.
Bind trailing parameters to an array.
Rest replaces the need for arguments
and addresses common cases more directly.
Let + Const
Block-scoped binding constructs.
let
is the new var
.
const
is single-assignment.
Static restrictions prevent use before assignment.
function someFunction() {
{
let x;
{
// this is ok since it's a block scoped name
const x = "beyond";
// error, was just defined with `const` above
x = "bangs"; // bangs: 刘海
}
// this is ok since it was declared with `let`
x = "bar";
// error, already declared above in this block
// 禁止重复定义
let x = "inner";
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/147c456d9cbf718805997d4a42c27259.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8e88dd444946658549d0b49c47cc1ec8.png)
CLR(公共语言运行库,Common Language Runtime) 和 Java虚拟机 一样也是一个运行时环境,
是一个可由多种编程语言使用的运行环境。
CLR的核心功能包括:内存管理、程序集加载、安全性、异常处理和线程同步,
可由面向CLR的所有语言使用。
并保证应用和底层操作系统之间必要的分离。
CLR是.NET Framework的主要执行引擎。
-
LINQ
(语言集成查询)
Iterators + For..Of
Iterator objects enable custom iteration like CLR IEnumerable or Java Iterable.
Generalize for..in
to custom iterator-based iteration with for..of
.
Don’t require realizing an array, enabling lazy design patterns like LINQ.
for of 循环有很多优点:
1. 不像for...in一样只遍历键名(甚至包括原型链上的键 ???Excuse Me???)
2. 不像foreach不能跳出循环
3. for...of为各种数据结构提供了一个统一的遍历方法
什么是 duck-typed ?
“当看到一只鸟 走起来像鸭子、游起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为 鸭子。”
我们并不关心对象是什么类型,到底是不是真正的鸭子,我们只关心行为。
Iteration is based on these duck-typed interfaces
(using TypeScript type syntax for exposition only)
(这儿只是用一下TypeScript的语法进行一下阐述而已啦~):