Maison  >  Article  >  interface Web  >  具体介绍JavaScript6的新特性

具体介绍JavaScript6的新特性

黄舟
黄舟original
2017-03-16 14:36:411642parcourir

JavaScript 6里都有啥新鲜东西?让我们一起来看看JavaScript 6的一些新特性。

  • letconst (用来定义block-local变量), 程序过程中的function

  • 解构: let {x, y} = pt; let [s, v, o] = triple();
    (前提是let pt = {x:2, y:-5})

  • 缺省参数值: function f(x, y=1, z=0) {⋯}

  • 其它参数: function g(i, j, ...r) { <a href="http://www.php.cn/wiki/135.html" target="_blank">return</a> r.slice(i, j); }
    (不需要再使用 arguments )。

  • 数据展开: let a = [0,1,2,3], o = <a href="http://www.php.cn/wiki/165.html" target="_blank">new</a> Something(...a);。也可以用于数组字面量: [1, ...<a href="http://www.php.cn/wiki/1041.html" target="_blank">array</a>, 4].

  • 对象简写:
    let one = 1; { one, func_one() {return 1;}, ['<a href="http://www.php.cn/wiki/1051.html" target="_blank">key</a> ' + one]: 1 }.

  • 函数简写 (a) => a * a 效果等同
    (function(a) { return a * a; }).bind(this)

  • map, setlet m = new Map(); m.set(key, value); m.has(key); m.get(key).
    还包括 .<a href="http://www.php.cn/wiki/917.html" target="_blank">clear</a>().<a href="http://www.php.cn/wiki/1298.html" target="_blank">delete</a>().<a href="http://www.php.cn/wiki/127.html" target="_blank">forEach</a>().keys().

  • 弱map: let map = new WeakMap()。当有循环引用时使用它。同理new WeakSet()

  • promise: new Promise((resolve, reject) => {…}).

    • 当 promise.then(value => {…})时,resolve(valueOrPromise) 返回承诺的值 (或者是一个新的promise,形成链式调用)

    • promise.then(…).then(…).catch(error => {…})reject(new Error(…))中断promise

    • 快速 promise 创建: Promise.resolve(value)Promise.reject(error).

    • 迭代: Promise.<a href="http://www.php.cn/wiki/1483.html" target="_blank">all</a>(listOfPromises).then(listOfValues => …),
      Promise.race(listOfPromises).then(valueThatResolvedFirst => …)

  • 代理: let obj = new Proxy(proto, han<a href="http://www.php.cn/wiki/596.html" target="_blank">dl</a>er).
    简单说: 使用类对象的元素进行重载(能够带来所有可访问的属性。)

  • 生成器function* gen() { yield 1; yield 2; }
    事实上,gen() 会返回一个含有 <a href="http://www.php.cn/wiki/1071.html" target="_blank">next</a>() 函数的对象。

  • 循环: for (var [key, val] of items(x)) { alert(key + ',' + val); }

  • 类定义中使用<a href="http://www.php.cn/wiki/166.html" target="_blank">extends</a><a href="http://www.php.cn/code/8202.html" target="_blank">super</a>, 和 <a href="http://www.php.cn/wiki/188.html" target="_blank">static</a>:

    class Point extends Base {
      constructor(x,y) {
        super();
        this[px] = x, this[py] = y;
        this.r = function() { return Math.sqrt(x*x + y*y); }
      }
      get x() { return this[px]; }
      get y() { return this[py]; }
      proto_r() { return Math.sqrt(this[px] * this[px] +
          this[py] * this[py]); }
      equals(p) { return this[px] === p[px] &&
          this[py] === p[py]; }
    }
  • 符号(Symbol)对象,创建私有的key,可用于map和类中(私有成员
    members)。

    let a = Map();
    {
      let k = Symbol();
      a.set(k, &#39;value&#39;);
      // 这里你可以访问和设置&#39;value&#39;,比如a.get(k)。
    }
    //这里不行,k是不可见的。
  • 模块化:

    module math {
      export function sum(x, y) {
        return x + y;
      }
      export var pi = 3.141593;
    }
    
    import {sum, pi} from math;
    alert(sum(pi,pi));
  • 模板式字符串: 可以多行,并能嵌入变量。
    `You are ${age} years old.`.

    // 多行字符串
    re`line1: (words )*
    line2: \w+`
    
    // It desugars to:
    re({raw:&#39;line1: (words )*\nline2: \w+&#39;,
        cooked:&#39;line1: (words )*\nline2: \w+&#39;})
  • 类型化数组

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn