ES2019

ES2019

tc39 Finished Proposals

ES2019

从表中可以看到已经有多个特性加到了 ES2019 中。

Optional catch binding

github.com/tc39/propos…

err 变成 optional 的,可以省略 catch 后的 括号和错误对象:

try {
  // tryCode
} catch {
  // catchCode
}
复制代码

Previous

之前 try...catch 是这样的:

try {
  // tryCode
} catch(err) {
  // catchCode
}
复制代码
  • tryCode:(必须)尝试执行的代码
  • err:(必须)指定局部变量应用的错误
  • catchCode:(可选)捕获错误的代码

比如:

try {
  throw new Error('报错啦报错啦');
} catch(e) {
  console.log(e); // Error: 报错啦报错啦
}
复制代码

有的时候我们只需要捕获错误但是无需知道错误信息,err 就显得没必要的。

JSON superset

github.com/tc39/propos…

允许 未转义的 U + 2028 行分隔符和 U + 2029 段分割符直接出现在字符串中,不会出现异常。

Previous

之前,JSON的某些字符 \u2028 \u2029 会导致 Javascript 语法错误。

eval('"\u2028"'); // SyntaxError: Unexpected
复制代码

我们的解决方法是对 \u2028 \u2029 进行转义,比如:

str.Replace('\u2028', '\\u2028')
复制代码

Symbol.prototype.description

github.com/tc39/propos…

Symbol.prototype.description | MDN

可以通过 description 方法获取 Symbol 的描述:

const name = Symbol('My name is axuebin');
console.log(name.description); // My name is axuebin
console.log(name.description === 'My name is axuebin'); // My name is axuebin
复制代码

Previous

我们知道,Symbol 的描述只被存储在内部的 [[Description]],没有直接对外暴露,我们只有调用 SymboltoString() 时才可以读取这个属性:

const name = Symbol('My name is axuebin');
console.log(name.toString()); // Symbol(My name is axuebin)
console.log(name); // Symbol(My name is axuebin)
console.log(name === 'Symbol(My name is axuebin)'); // false
console.log(name.toString()) === 'Symbol(My name is axuebin)'); // true

复制代码

在执行 console.log(name) 的时候也打印了描述信息,是因为这里隐式地执行了 toString(),在代码里这样是不行的。

Function.prototype.toString revision

github.com/tc39/Functi…

现在 foo.toString() 可以返回精确字符串,包括空格和注释等。

Object.fromEntries

github.com/tc39/propos…

Object.fromEntries() | MDN

该方法把键值对列表转换为一个对象,可以看作是 Object.entries() 的反向方法。

  • Arguments:(Array): 键值对。
  • Returns:(Object): 对象。
const arr = Object.entries({ name: 'axuebin', age: 27 });
console.log(arr); // ["name", "axuebin"], ["age', 27]]

const obj = Object.fromEntries(arr);
console.log(obj); // { name: 'axuebin', age: 27 }
复制代码

lodash_.fromPairs 具有一样的功能。

const obj = _.fromPairs(['name', 'axuebin'], ['age', 27]);
console.log(obj); // { name: 'axuebin', age: 27 }
复制代码

Well-formed JSON.stringify

github.com/tc39/propos…

更友好的 JSON.stringify,对于一些超出范围的 Unicode,为其输出转义序列,使其成为有效 Unicode

JSON.stringify('\uDF06\uD834'); // '"\\udf06\\ud834"'
JSON.stringify('\uDEAD'); // '"\\udead"'
复制代码

Previous

JSON.stringify('\uDF06\uD834'); // '"��"'
JSON.stringify('\uDEAD'); // '"�"'
复制代码

String.prototype.{trimStart,trimEnd}

github.com/tc39/propos…

String.prototype.trimStart() | MDN

String.prototype.trimEnd() | MDN

分别去除字符串前后的空格,生成新的字符串。

const str = '    axuebin    ';
console.log(str.trimStart()); // 'axuebin    '
console.log(str.trimEnd()); // '    axuebin'
console.log(str); // '    axuebin    '
复制代码

Array.prototype.{flat,flatMap}

github.com/tc39/propos…

Array.prototype.flat() | MDN

Array.prototype.flatMap() | MDN

还记得这样一道笔试题么,给你一个多维数组,把它拍平!

const arr = [1, [2, [3, [4, [5, 6]]]]];
arr.flat(); // [1, 2, [3, [4, [5, 6]]]]
arr.flat(1); // [1, 2, [3, [4, [5, 6]]]]
arr.flat(2); // [1, 2, 3, [4, [5, 6]]]
arr.flat(3); // [1, 2, 3, 4, [5, 6]]
arr.flat(4); // [1, 2, 3, 4, 5, 6]
复制代码
const arr = [[1, 2, 3], [4, 5]];
arr.flatMap(item => item); [1, 2, 3, 4, 5];
复制代码

是不是很方便...

暂时就这些啦

原文链接:github.com/axuebin/art…

猜你喜欢

转载自juejin.im/post/5c52a645f265da2ddc3c4bd8