ES6_30分钟速读_新特性




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





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.






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.





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";
  }
}


CLR(公共语言运行库,Common Language Runtime) 和 Java虚拟机 一样也是一个运行时环境,

是一个可由多种编程语言使用的运行环境。

CLR的核心功能包括:内存管理程序集加载、安全性、异常处理线程同步

可由面向CLR的所有语言使用。

并保证应用和底层操作系统之间必要的分离。

CLR是.NET Framework的主要执行引擎。 



LINQ

 

(语言集成查询)

 
LINQ(Language Integrated Query) 语言集成查询是一组用于 c# Visual Basic 语言的扩展。
它允许编写C#或者Visual Basic代码 以查询数据库 相同的方式 操作 内存数据。



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的语法进行一下阐述而已啦~):


  
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值