• JavaScript、ES5和ES6的介绍和区别


    JavaScript由三部分组成:

    1. ECMAScript(核心)
    2. DOM(文档对象模型)
    3. BOM (浏览器对象模型)

    ES5(ECMAScript第五个版本)

    1. strict模式

    严格模式,限制一些用法,'use strict';

    1. Array增加方法

    增加了every、some 、forEach、filter 、indexOf、lastIndexOf、isArray、map、reduce、reduceRight方法

    PS: 还有其他方法 Function.prototype.bind、String.prototype.trim、Date.now

    1. Object方法

    Object.getPrototypeOf
    Object.create
    Object.getOwnPropertyNames
    Object.defineProperty
    Object.getOwnPropertyDescriptor
    Object.defineProperties
    Object.keys
    Object.preventExtensions / Object.isExtensible
    Object.seal / Object.isSealed
    Object.freeze / Object.isFrozen

    ES6(ECMAScript第六个版本)
    ECMAScript6在保证向下兼容的前提下,提供大量新特性
    1.块级作用域 关键字let, 常量const
    2.函数参数 - 默认值、参数打包、 数组展开(Default 、Rest 、Spread)

    //Default
    function findArtist(name='lu', age='26') {
        ...
    }
    
    //Rest
    function f(x, ...y) {
      // y is an Array
      return x * y.length;
    }
    f(3, "hello", true) == 6
    
    //Spread
    function f(x, y, z) {
      return x + y + z;
    }
    // Pass each elem of array as argument
    f(...[1,2,3]) == 6
    

    3.箭头函数 Arrow functions
    (1).简化了代码形式,默认return表达式结果。
    (2).自动绑定语义this,即定义函数时的this。

    4.字符串模板 Template strings

    var name = "Bob", time = "today";
    `Hello ${name}, how are you ${time}?`
    // return "Hello Bob, how are you today?"
    

    5.Class
    Class,有constructor、extends、super,但本质上是语法糖(对语言的功能并没有影响,但是更方便程序员使用)。

    class Artist {
        constructor(name) {
            this.name = name;
        }
    
        perform() {
            return this.name + " performs ";
        }
    }
    
    class Singer extends Artist {
    
        constructor(name, song) {
            super.constructor(name);
            this.song = song;
        }
    
        perform() {
            return super.perform() + "[" + this.song + "]";
        }
    }
    
    let james = new Singer("Etta James", "At last");
    james instanceof Artist; // true
    james instanceof Singer; // true
    
    james.perform(); // "Etta James performs [At last]"
    

    6.Modules

    ES6的内置模块功能借鉴了CommonJS和AMD各自的优点:

    (1).具有CommonJS的精简语法、唯一导出出口(single exports)和循环依赖(cyclic dependencies)的特点。

    (2).类似AMD,支持异步加载和可配置的模块加载。

    // lib/math.js
    export function sum(x, y) {
      return x + y;
    }
    export var pi = 3.141593;
    
    // app.js
    import * as math from "lib/math";
    alert("2π = " + math.sum(math.pi, math.pi));
    
    // otherApp.js
    import {sum, pi} from "lib/math";
    alert("2π = " + sum(pi, pi));
    
    1. Proxies

    使用代理(Proxy)监听对象的操作,然后可以做一些相应事情。

    var target = {};
    var handler = {
      get: function (receiver, name) {
        return `Hello, ${name}!`;
      }
    };
    
    var p = new Proxy(target, handler);
    p.world === 'Hello, world!';
    
    可监听的操作: get、set、has、deleteProperty、apply、construct、getOwnPropertyDescriptor、
    defineProperty、getPrototypeOf、setPrototypeOf、enumerate、ownKeys、preventExtensions、isExtensible。
    

    8.Promises

    Promises是处理异步操作的对象,使用了 Promise 对象之后可以用一种链式调用的方式来组织代码,
    让代码更加直观(类似jQuery的deferred 对象)。

    function fakeAjax(url) {
      return new Promise(function (resolve, reject) {
        // setTimeouts are for effect, typically we would handle XHR
        if (!url) {
          return setTimeout(reject, 1000);
        }
        return setTimeout(resolve, 1000);
      });
    }
    
    // no url, promise rejected
    fakeAjax().then(function () {
      console.log('success');
    },function () {
      console.log('fail');
    });
    

    参考
    http://caibaojian.com/toutiao/6864

    附:
    ES6语法快速上手
    https://segmentfault.com/a/1190000005742091
    在React/React Native中ES5与ES6的对比不同点
    http://blog.csdn.net/changsimeng/article/details/62883952

  • 相关阅读:
    nginx proxy_cache 缓存配置[转]
    MongoDB使用小结:一些常用操作分享
    PHP读取Mongodb数据报错,Cannot natively represent the long 8331412483000 on this platform
    MongoDB 学习笔记(python操作)
    Python 中 os.path模板
    Python 优雅的操作字典【转】
    MongoDB 一对多关系建模
    nginx之location配置
    MongoDB安装Windows服务
    .net4.0 请求HTTPS出错:未能创建 SSL/TLS 安全通道
  • 原文地址:https://www.cnblogs.com/fozero/p/6959798.html
Copyright © 2020-2023  润新知