基础变化

  1. String类型新增了三个方法,不必使用indexOf来判断一个字符串是否在另一个字符串内

    //String changes
    var a = "Hello world";
    var b = "Hello";
    var c = "world";
    function includes(source, dest) {
    return source.indexOf(dest) > -1;
    }
    function startsWith(source, dest) {
    return source.slice(0, dest.length) === dest;
    }
    function endsWith(source, dest) {
    return source.slice(source.length - dest.length, source.length) === dest;
    } var msg = "Hello world!"; console.log("msg startsWith Hello: ", msg.startsWith("Hello")); // true
    console.log("msg endsWith !: ", msg.endsWith("!")); // true
    console.log("msg includes o: ", msg.includes("o")); // true console.log("msg startsWith o: ", msg.startsWith("o")); // false
    console.log("msg endsWith world!: ", msg.endsWith("world!")); // true
    console.log("msg includes x: ", msg.includes("x")); // false
  2. Object.is方法用来判断两个参数是否相等,与全等(===)类似只是在+0和-0上以及NaN与NaN的判断上与全等不同
    console.log(+0 == -0);              // true
    console.log(+0 === -0); // true
    console.log(Object.is(+0, -0)); // false console.log(NaN == NaN); // false
    console.log(NaN === NaN); // false
    console.log(Object.is(NaN, NaN)); // true console.log(5 == 5); // true
    console.log(5 == "5"); // true
    console.log(5 === 5); // true
    console.log(5 === "5"); // false
    console.log(Object.is(5, 5)); // true
    console.log(Object.is(5, "5")); // false
  3. let声明,let与var的作用相同,只是以let声明的变量的作用域在当前的{}块内
    function getValue(condition) {
    
        if (condition) {
    let value = "blue"; // other code return value;
    } else { // value doesn't exist here return null;
    } // value doesn't exist here
    }
  4. const关键字用来声明常量,常量一旦赋值就无法改变,其他的赋值表达式都会被忽略
  5. 解构赋值,引入解构赋值可以方便的从复杂的对象中取得所需的属性值
    var options = {
    repeat: true,
    save: false,
    rules: {
    custom: 10,
    }
    }; // later var { repeat, save, rules: { custom }} = options; console.log(repeat); // true
    console.log(save); // false
    console.log(custom); //

  1. 类声明语法,目前许多前端框架比如dojo、extJs使用辅助设计使得Javascript看起来支持“类”,基于以上目的ES6引入类体系;目前在Chrome使用class关键字必须使用严格模式

    //class declaration
    function PersonType(name) {
    this.name = name;
    } PersonType.prototype.sayName = function() {
    console.log(this.name);
    }; let person = new PersonType("Nicholas");
    person.sayName(); // outputs "Nicholas" console.log(person instanceof PersonType); // true
    console.log(person instanceof Object); // true (function(){
    'use strict';
    class PersonClass {
    constructor(name) {
    this.name = name;
    }
    sayName() {
    console.log(this.name);
    }
    } let person = new PersonClass("Nicholas");
    person.sayName(); // outputs "Nicholas" console.log(person instanceof PersonClass);
    console.log(person instanceof Object);
    })()
  2. 属性访问器,通过使用get和set关键字来声明属性(Attribute),在ES5中需要借助Object.defineProperty来声明属性访问器
    //Accessor Properties
    (function(){
    'use strict';
    class PersonClass {
    constructor(name) {
    this.name = name;
    }
    get Name(){
    return this.name;
    }
    set Name(value){
    this.name = value;
    }
    } let person = new PersonClass("Nicholas");
    console.log('person.Name: ', person.Name) // outputs "Nicholas"
    })()
  3. 静态成员,ES5或者之前的代码通过在构造函数中直接定义属性来模拟静态成员;ES6则只需要在方法名前面加上static关键字
    //ES5
    function PersonType(name) {
    this.name = name;
    } // static method
    PersonType.create = function(name) {
    return new PersonType(name);
    }; // instance method
    PersonType.prototype.sayName = function() {
    console.log(this.name);
    }; var person = PersonType.create("Nicholas"); //ES6
    //Static Members
    (function(){
    'use strict';
    class PersonClass {
    constructor(name) {
    this.name = name;
    } sayName() {
    console.log(this.name);
    } static create(name) {
    return new PersonClass(name);
    }
    } let person = PersonClass.create("Nicholas");
    console.log(person);
    })()
  4. 继承,ES5中需要借助prototype属性而ES6中引入extends关键字来实现继承
    //Handling Inheritance
    (function(){
    'use strict';
    class PersonClass {
    constructor(name) {
    this.name = name;
    }
    } class Developer extends PersonClass {
    constructor(name, lang) {
    super(name);
    this.language = lang;
    }
    } var developer = new Developer('coder', 'Javascript');
    console.log("developer.name: ", developer.name);
    console.log("developer.language: ", developer.language);
    })()

模块机制

  当前关于JS的模块化已有两个重要的规范CommonJs和AMD,但毕竟不是原生的模块化,所以ES6中引入模块化机制,使用export和import来声明暴露的变量和引入需要使用的变量

  

Iterator和Generator

  Iterator拥有一个next方法,该方法返回一个对象,该对象拥有value属性代表此次next函数的值、done属性表示是否拥有继续拥有可返回的值;done为true时代表没有多余的值可以返回此时value为undefined;Generator函数使用特殊的声明方式,generator函数返回一个iterator对象,在generator函数内部的yield关键字声明了next方法的值

//Iterator & Generator
// generator
function *createIterator() {
yield 1;
yield 2;
yield 3;
} // generators are called like regular functions but return an iterator
var iterator = createIterator();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());

Promise

  ES6引入原生的Promise对象,Promise构造函数接受一个方法作为参数,该方法中可以调用resolve和reject方法,分别进入fulfill状态和fail状态

// Promise
var getJSON = function(url) {
var promise = new Promise(function(resolve, reject){
var client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.send(); function handler() {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
debugger;
resolve(this.responseText);
} else {
reject(new Error(this.statusText));
}
};
}); return promise;
}; getJSON("https://gis.lmi.is/arcgis/rest/services/GP_service/geocode_thjonusta_single/GeocodeServer?f=json").then(function(json) {
console.log('Contents: ' + json);
}, function(error) {
console.error('Error: ', error);
});

Proxy

  顾名思义用来作为一个对象或函数的代理。Proxy构造函数接受两个参数:target用来被封装的对象或函数、handler拥有一系列方法,重写这些方法以便当调用这些操作时会进入重写的方法中

•handler.getPrototypeOf
•handler.setPrototypeOf
•handler.isExtensible
•handler.preventExtensions
•handler.getOwnPropertyDescriptor
•handler.defineProperty
•handler.has
•handler.get
•handler.set
•handler.deleteProperty
•handler.enumerate
•handler.ownKeys
•handler.apply
•handler.construct

handler.getPrototypeOf
handler.setPrototypeOf
handler.isExtensible
handler.preventExtensions
handler.getOwnPropertyDescriptor
handler.defineProperty
handler.has
handler.get
handler.set
handler.deleteProperty
handler.enumerate
handler.ownKeys
handler.apply
handler.construct

参考资料:

Understanding ECMAScript 6

ECMAScript 6 入门

初探ECMAScript6的更多相关文章

  1. TypeScript初探

    TypeScript初探 TypeScript什么? 官方给的定义:TypeScript是一种由微软开发的自由和开源的编程语言,它是JavaScript类型的超集,可以编译成纯JavaScript,本 ...

  2. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  3. CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探

    CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...

  4. 从273二手车的M站点初探js模块化编程

    前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...

  5. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  6. .NET文件并发与RabbitMQ(初探RabbitMQ)

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...

  7. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  8. 【手把手教你全文检索】Apache Lucene初探

    PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...

  9. Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用

    一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...

随机推荐

  1. 海量数据相似度计算之simhash和海明距离

    通过 采集系统 我们采集了大量文本数据,但是文本中有很多重复数据影响我们对于结果的分析.分析前我们需要对这些数据去除重复,如何选择和设计文本的去重算法?常见的有余弦夹角算法.欧式距离.Jaccard相 ...

  2. C# ToString("x2")的理解

    1).转化为16进制. 2).大写X:ToString("X2")即转化为大写的16进制. 3).小写x:ToString("x2")即转化为小写的16进制. ...

  3. iOS开发-automaticallyAdjustsScrollViewInsets属性

    iOS开发-automaticallyAdjustsScrollViewInsets属性 Available in iOS 7.0 and later. 简单点说就是automaticallyAdju ...

  4. Java中super的几种用法并与this的区别

    1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base"); ...

  5. 关于String中+与StringBuilder的问题

      字符串连接可以通过两种方法实现,其中一种是在Java中提供的一个StringBuilder类(这个类只在J2SE5及以上版本提供,以前的版本使用StringBuffer类). 字符串是Java程序 ...

  6. 对button或radiobutton制作样式

    制作样式在drawable中进行 (cheakbox)在样式中点击变换图片,<selector> <item android:state_checked="true&quo ...

  7. mybatis中表与表之间的关联

    第三天 1.mybatis处理表与表之间的关系? 比如要在帖子回复表里显示其它两张相关联表的信息. 处理的第一种方式: 1)主要的数据实体类是ReplyInfo,相关联的实体表的数据是TitleInf ...

  8. Eclipse问题集锦

    1.SDK版本过低的问题. 现象: 更新SDK后,每次进入Eclipse,都会提示说需要23.0.0版本的SDK,当前的22.6.0版本的SDK版本过低:然而,确认更新后,结果却是说没有任何更新的东东 ...

  9. 小试牛刀--利用豆瓣API爬取豆瓣电影top250

    最近得赶进度爬点东西,对于豆瓣,它为开发者提供了API,目前是v2版本,目前key不对个人开放,但是可以正常通过其提供的API获取数据.豆瓣V2版API权限分3类:公开.高级.商务,我们用开放基本数据 ...

  10. Area区域

    1.mvc4.0新增的area区域机制,可以协助你在架构较为大型的项目,让独立性较高的部分功能独立成一个MVC子网站,以降低网站与网站之间的耦合性,也可以通过area的切割,让多人同时开发同一个项目时 ...