ES2018(ES9) Puzzlers

ES2018(ES9) 马上发布,新特性包括:异步迭代器、对象展开运算符、Promise.prototype.finally、非转义序列的模板字符串、正则表达式 s/dotAll 模式、等等...

本网站主要用于测试你对 ES2018(ES9) 的掌握情况,答案可在 esnext.justjavac.com 找到,前端交流 QQ 群: 599243863

( 0 / 0 ) N/A
  1. 以下代码的执行结果?

    let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
    console.log(z);
    送分题,不解释
  2. 以下代码的执行结果?

    let { ...obj } = Object.create({ x: 1 });
    console.log(obj.x);
    对象的剩余属性赋值时,只赋值自己的属性,不赋值原型链上的属性。
  3. 以下两段代码在功能上是否相等?

    function fn1(obj) {
      let { x, y, ...z } = obj;
      return { x, y, z };
    }
    
    function fn2(obj) {
      let { x, ...n } = obj;
      let { y, ...z } = n;
      return { x, y, z };
    }
    同上。
  4. 以下代码的执行结果?

    let { x, y, ...z } = null;
    console.log(z);
    TypeError: Cannot destructure property `x` of 'undefined' or 'null'. 运行时错误。
  5. 以下代码的执行结果?

    let obj = { x: 1, y: 2, a: 3, b: 4 };
    let { ...x, y, z } = obj;
    console.log(x);
    SyntaxError: Rest element must be last element
  6. 以下代码会抛出异常吗?

    let obj = { x: 1, y: 2, a: 3, b: 4 };
    let objWithXGetter = { ...obj, get x() { throw new Error() } };
    上面代码等价于:
    let objWithXGetter = {};
    
    Object.assign(objWithXGetter, a);
    
    Object.defineProperty(objWithXGetter, "x", {
      get(){ throw new Error() },
      enumerable : true,
      configurable : true
    });
    不会抛出异常。
  7. 以下代码会抛出异常吗?

    let obj = { x: 1, y: 2, a: 3, b: 4 };
    let objWithXGetter = { 
      ...obj, 
      ...{ get x() { throw new Error() } } 
    };
    .x 被调用了,所以抛出了异常
  8. 以下代码的执行结果?

    let emptyObject = { ...null, ...undefined };
    let emptyArray = [...null, ...undefined];
    对象会忽略 nullundefined,数组会抛异常 WTF
  9. 以下代码的执行结果?

    let x = { a: 1, b: 2 };
    let y = [ ...x ];
    TypeError: x is not iterable
  10. 以下代码的执行结果?

    let x = { a: 1, b: 2, length: 2 };
    let y = [ ...x ];
    TypeError: x is not iterable
  11. 以下代码的执行结果?

    let x = [ 1, 2 ];
    let { ...y } = x;
    console.log(y);
    x 被转换成对象。
  12. 以下代码的执行结果?

    let x = { a: 1 };
    let o = { 
      __proto__: null, 
      b: 2,
      ...x
    };
    console.log(o);
    这道题的考点与 ES2018 的对象扩展运算符没有任何关系。 let o = { __proto__: null }let o = Object.create(null) 一样,都是创建一个真正的空对象,比 let o = {},因为这个对象没有原型链。
  13. 以下代码的执行结果?

    let x = `\u{55}`;
    x.length;
    \u开始的为 unicode 转义字符。
  14. 以下代码的执行结果?

    let x = `\unicode`;
    x.length;
    SyntaxError: Invalid Unicode escape sequence 非法的 Unicode 转义序列。
  15. 以下代码的执行结果?

    let x = String.raw`\u{55}`;
    x.length;
    String.raw 返回字符串的原始表示
  16. 以下代码的执行结果?

    let x = String.raw`\unicode`;
    x.length;
    在 ES2018 中修订了 String.raw 的行为,非法的转义序列不再抛出异常
  17. 以下代码的执行结果?

    function tag(str) {
      return str[0].toUpperCase();
    }
    
    tag`justjavac`;
    这个是 ES6 的知识点:标签函数的第一个参数包含一个字符串值的数组。其余的参数与表达式相关。
  18. 以下代码的执行结果?

    function tag(str) {
      return str[0];
    }
    
    tag`\unicode`;
    ECMAScript 2018 移除了带标签的模版字符串中转义序列的语法限制。对于非法的转义序列(Unicode、二六进制、八进制)返回 undefined
  19. 以下代码的执行结果?

    let regex = /^.$/;
    
    regex.test('道');
    regex.test('😀');
    
    这个不是 ES2018 知识点,是 ES2015 的。对于 Unicode,ES5 中提供了 charCodeAt() 方法来返回指定索引处字符的 Unicode 数值,但是 Unicode 编码单元 > 0x10000 的除外,ES2015 中又增加了一个新的方法 codePointAt() 可以返回大于 0x10000 字符串的数值。 JavaScript 虽然默认支持 Unicode,但是 JavaScript 的编码方式是 UCS-2,所有字符都是 2 个字节,如果是 4 个字节的字符,会当作两个双字节的字符处理。在 ES6 中,正则表达式的 u 标识启用对 Unicode 特殊字符的支持。因此我们使用 let regex = /^.$/u 可以得到期望的结果 true true
  20. 以下代码 true 和 false 的个数?

    let regex = /./;
    
    regex.test('\n');
    regex.test('\r');
    regex.test('\v');
    regex.test('\f');
    regex.test('\u{2028}');
    regex.test('\u{2029}');
    regex.test('\u{0085}');
    在正则表达式中 . 可以匹配换行符以外的任意字符,换行符包括 \n\r\u{2028}\u{2029}。 还有一些其它字符,虽然也可以作为一行的开始,但是 . 可以匹配,包括 \v\f\u{0085}。 ES2018 为正则表达式增加了 s/dotAll 标志,可以让 . 匹配任意字符,所以 let regex = /./s 可以使以上代码都返回 true