es8

es8

es8的五个特性:

  1. 字符串补全 String.prototype.padStart(length [, string])和String.prototype.padEnd(length [, string])`

    • padStart

      'abc'.padStart(1); // 'abc'
      'abc'.padStart(1, 'def'); // 'abc'
      // 只要第一个参数不大于原字符串长度就会返回原字符串

      'abc'.padStart(4); // ' abc'(注意:前面有一个空格)
      'abc'.padStart(4, 'def'); // 'dabc'
      'abc'.padStart(8, 'def'); // 'defdeabc'
      // 只要第一个参数大于原字符串长度,就会在前面补上第二个参数的内容,没有第二个参数则补空格。

    • padEnd

      'abc'.padEnd(1); // 'abc'
      'abc'.padEnd(1, 'def'); // 'abc'
      // 只要第一个参数不大于原字符串长度就会返回原字符串

      'abc'.padEnd(4); // 'abc '(注意:后面有一个空格)
      'abc'.padEnd(4, 'def'); // 'abcd'
      'abc'.padEnd(8, 'def'); // 'abcdefde'
      // 只要第一个参数大于原字符串长度,就会在后面补上第二个参数的内容,没有第二个参数则补空格。

  2. Object.values()和Object.entries();

    • 对象

      const obj1 = {
      'x': 'xx',
      'y': 'yy'
      }
      const obj2 = {
      10: 'xx',
      5: 'yy'
      }

      Object.values(obj1); // ['xx', 'yy']
      Object.entries(obj1); // [['x', 'xx'], ['y', 'yy']]

      Object.values(obj2); // ['yy', 'xx']
      Object.entries(obj2); // [[5, 'yy'], [10, 'xx']]

    • 数组

      const arr = ['x', 'y'];

      Object.values(arr); // ['x', 'y']
      Object.entries(arr); // [['0', 'x'], ['1', 'y']]

    • 字符串

      const str = ’xy';

      Object.values(str); // ['x', 'y']
      Object.entries(str); // [['0', 'x'], ['1', 'y']]

  3. 函数结尾可以使用逗号

    function foo(a, b,) {};
    foo(1, 2,);

  4. async...await

    async function f() {
    const n1 = await foo1();
    console.log(n1);
    }

    function foo1() {
    return new Promise((res, rej) => {
    setTimeout(() => {
    res(1);
    }, 1000);
    });
    }

    f(); // 1s后显示1

  5. Object.getOwnPropertyDescriptors

    • 只会返回对自身属性的描述,不会返回对继承属性的描述

      function Obj() {
      this.prop1 = 'prop1';
      }
      Obj.prototype.prop2 = 'prop2';

      const obj = new Obj();

      Object.defineProperty(obj, 'prop3', {
      value: 'prop3',
      enumerable: false,
      writable: false,
      configurable: false
      });

      Object.getOwnPropertyDescriptors(obj);
      // {
      prop1: {
      value: 'prop1',
      writable: true,
      enumerable: true,
      configurable: true
      },
      prop3: {
      value: 'prop3',
      writable: false,
      enumerable: false,
      configurable: false
      }
      }

posted @ 2017-10-18 10:23  日含  阅读(198)  评论(0)    收藏  举报