0、导言

最近从coffee切换到js,代码量一下子变大了不少,也多了些许陌生感。为了在JS代码中,更合理的使用ES6的新特性,特在此对ES6的特性做一个简单的总览。

1、模块(Module) --Chrome测试不可用

在ES6中,有class的概念,不过这只是语法糖,并没有解决模块化问题。Module功能则是为了解决模块化问题而提出的。

我们可以使用如下方式定义模块:

11_lib.js文件内容

// 导出属性和方法
export var PI = 3.1415926;
export function calcCircularArea(r){
return PI * r * r;
}

app.js文件内容

//导出所有,使用别名调用
import * as lib from '11_lib';
console.log(lib.calcCircularArea(2));
console.log(lib.PI); //导出属性和方法
import {calcCircularArea, PI} from '11_lib';
console.log(calcCircularArea(2));
console.log(PI);

2、模块加载器(Module Loaders) --Chrome测试不可用

既然用了定义module的规范,那么也就需要一个模块加载器,需要支持如下内容:

  1. 动态加载
  2. 状态隔离
  3. 全局命名空间隔离
  4. 编译钩子
  5. 嵌套虚拟化

System.import('11_lib').then(function(m) {
console.log(m.calcCircularArea(2));
console.log(m.PI);
});

3、图 + 集合 + 弱引用图 + 若引用集合(Map + Set + WeakMap + WeakSet)

在ES6中,新增了几种数据结构。

Map是一种类似于Object的结构,Object本质上是键值对的集合,但是只能是字符串当做键,所以有一定的使用限制。Map的话,则可以拿任意类型来作为key。具体使用如下:

var map = new Map();
var key = {key: 'hah'};
map.set(key, '1'); //设置key-value
map.set(key, 2); //对已有key进行设置,表示覆盖
console.log(map.get(key)); //获取key的值
console.log(map.size);//获取map的元素个数
map.has(key); //判断map中有指定的key
map.delete(key); //删除map中指定的key
map.clear(); //清空map

WeakMap和Map是比较类似的,唯一的区别是只接受对象作为键名(null除外),而且键名所指向的对象,不计入垃圾回收机制。

Set是一种类似于数组,但成员的值都是唯一(引用唯一)的一种数据结构。具体使用如下:

var set = new Set();//定义set
set.add(1).add(2).add(1).add('2'); //添加数据
console.log(set.size);//查看set中元素的数量,结果应该是3,因为重复添加不计算,2和'2'不等。
set.delete(1); //删除set的值(通过value删除)。
set.has(1); //set是否包含某个value
set.keys(); //返回set的所有key
set.values(); //返回set的所有value
set.clear();//清空set

WeakSet和Set也是比较类型的,和Set有两个区别,一个是成员只能是对象;二个是WeakSet是不可遍历的。

4、代理(Proxies) --Chrome测试不可用

代理允许用宿主的行为来创建对象,能够实现拦截,对象的虚拟化,日志和分析等功能。

5、数据类型Symbols

在ES5中,JS只有6中原始类型,在ES6中,新增了Symbols类型,成为了JS中的第7种原始类型。 该类型表示独一无二的值。使用如下:

var key = Symbol(); //定义Symbol对象 console.log(typeof key); //symbol ,表示为类型,而且不是string类型的。 key = Symbol('这是一个说明'); //可以在定义Symbol的时候,添加一个说明

Symbol不能与其他类型值进行运算,但是可以显式转换为字符串,和转换为布尔值

console.log(key.toString());
console.log(String(key)); if(key){
console.log('key is true');
}

在对象的内部,要使用Symbol值定义属性时,必须放在方括号中。

var obj = {
[key]: 'abc'
};

6、可以子类化的内置对象

在ES6中,我们可以自定义类型来继承内置对象,这个时候,如果要自定义构造函数,必须要在构造函数中调用super(),来呼叫父类的构造。

'use strict';
class MyArray extends Array {
// 如果要定义constuctor,那么就必须要使用super来执行父类的构造
constructor(){
super();
}
} var arr = new MyArray();
arr[1] = 12;
console.log(arr.length === 2);

7、新增的API(Math + Number + String + Array + Object APIs)

如下代码,一目了然:

//数字类api
Number.EPSILON; //增加常量e
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false //数学类api
Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2 //字符串类api
"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc" //数组api
Array.from(document.querySelectorAll('*')) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1, 2, 3].find(x => x == 3) // 3
[1, 2, 3].findIndex(x => x == 2) // 1
[1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2]
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c" //对象api
Object.assign(Point, { origin: new Point(0,0) })

8、二进制和八进制字面量(Binary and Octal Literals)

直接上示例:

var n1 = 0b111110101; //0b前缀,表示二进制字面量
console.log(n1); //输出的时候,直接用10进制展示 var n2 = 0o12345; //0o前缀,表示八进制字面量
console.log(n2);

9、承诺(Promises)

Promise是ES6中新增的异步编程库。

//使用承诺定义一个异步任务
var p = new Promise((resolve, reject)=>{
return setTimeout(function(){
reject('ok');
}, 2000);
}); p.then((data)=>{
console.log(data);
}, (data)=>{
console.log('error' + data);
}).then(()=>{
console.log('throw err');
throw 'Error';
}).catch(err => {
console.log(err);
});

10、参考资料

1、ECMAScript 6 features https://github.com/lukehoban/es6features

2、ECMAScript 6 入门 http://es6.ruanyifeng.com/

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

ES6入门系列三(特性总览下)的更多相关文章

  1. C# 互操作性入门系列(三):平台调用中的数据封送处理

    好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...

  2. [转]C# 互操作性入门系列(三):平台调用中的数据封送处理

    参考网址:https://www.cnblogs.com/FongLuo/p/4512738.html C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列( ...

  3. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  4. ActiveMQ入门系列三:发布/订阅模式

    在上一篇<ActiveMQ入门系列二:入门代码实例(点对点模式)>中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub & Sub),详细介绍了点对点 ...

  5. ES6 入门系列 (三) 尾递归

    递归我们不陌生, 那什么是尾递归呢? 为什么要用尾递归呢? 尾递归怎么用呢? 带着这三个问题我们来了解它, 我们知道递归非常耗费内存,一不小心就会发生‘栈溢出’, 相信你一定遇到过这个错误: stac ...

  6. ES6 入门系列 - 函数的扩展

    1函数参数的默认值 基本用法 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log( ...

  7. ES6入门系列四(测试题分析)

    0.导言 ES6中新增了不少的新特性,来点测试题热热身.具体题目来源请看:http://perfectionkills.com/javascript-quiz-es6/. 以下将一题一题来解析what ...

  8. ES6 入门系列 - let 和 const 命令

    let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = ; ; } a // ReferenceEr ...

  9. ES6 入门系列 (一)ES6的前世今生

    要学好javascript , ECMAScript标准比什么都强, ESMAScript标准已经用最严谨的语言和最完美的角度展现了语言的实质和特性. 理解语言的本质后,你已经从沙堆里挑出了珍珠,能经 ...

随机推荐

  1. 7.4 MVC vs MVP

    MVC(Model_view_contraller)"模型_视图_控制器". MVC应用程序总是由这三个部分组成.Event(事件)导致Controller改变Model或View ...

  2. spring简介

    在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...

  3. 重新开源UDS

    这个题目起得很纠结. 因为很多人都知道UDS本来就是开源,我只不过改了一些东西,然后重新发布,所以不算重新开源. 要说重新发布也不对.因为老早这东西就发布了. 最后我想,这个东西已经很久没更新过了,也 ...

  4. 工作随笔——UIButton的EdgeInsets + Swift中的正则表达式;

    1.UIButton的EdgeInsets UIButton的EdgeInsets方法,是用来设置title和image对于上左下右四个方向的偏移,但是很奇怪的是,刚开始只有Image,titile也 ...

  5. iOS 直播类APP开发流程分解:

    1 . 音视频处理的一般流程: 数据采集→数据编码→数据传输(流媒体服务器) →解码数据→播放显示1.数据采集:摄像机及拾音器收集视频及音频数据,此时得到的为原始数据涉及技术或协议:摄像机:CCD.C ...

  6. 图片下载缓存防止OOM

    一  ImageManager ImageMemoryCache(内存缓存).ImageFileCache(文件缓存)    关于Java中对象的软引用(SoftReference),如果一个对象具有 ...

  7. Hasor-Core v0.0.4 & Web v0.0.3 发布

    “Hasor是一款开源的 Java 应用开发框架.它是围绕 Guice 为核心创建的一系列模块组合而成.使用Hasor 会加快软件软件开发效率并降低开发成本.目前 Hasor包含了 Hasor-Cor ...

  8. 微软再次要求Google审查官方链接 称将进行调查

    之前代表微软向Google发出DMCA删除通知的反盗版公司再次要求Google审查Microsoft.com官网链接.微软对此表示将进行调查,已经要求反盗版公司停止以微软的名义发出DMCA通知. 仅仅 ...

  9. 微软四十周年 Microsoft’s 40th anniversary

    比尔-盖茨在4月3日给微软全体员工写了这封邮件,原文是英文,我们翻译了中文.图片是后加上的. 明天将是特殊的一天:微软的40周年纪念日. Tomorrow is a special day: Micr ...

  10. Nim教程【十】

    openarray类型 注意:openarray类型只能用于参数 固定大小的数组虽然性能不错,但过于呆板,使用取来不是很方便 对于一个方法来说,传入参数如果是一个数组,最好是不要限制数组的长度 也就是 ...