// Polyfill
function at(n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if(n < 0) n += this.length;
// OOB access is guaranteed to return undefined
if(n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];
}
二、顶层 await 0 i1 T+ T8 i9 u- |3 V# f, R await 都得用 async 函数包裹大家肯定都知道,这个限制导致我们不能在全局作用域下直接使用 await,必须得包装一下。有了这个提案以后,大家就可以直接在顶层写 await 了,算是一个便利性的提案。目前该提案已经进入阶段 4,板上钉钉会发布。另外其实 Chrome 近期的更新已经支持了该功能。* m* S% A% L2 R, b, u 8 G0 |; j3 v1 ?9 `8 X三、Error Cause C) X9 ^% G, [5 N( N 这个语法主要帮助我们便捷地传递Error。一旦可能出错的地方一多,实际就不清楚错误到底是哪里产生的。如果希望外部清楚的知道上下文信息的话,需要封装以下error。 7 j( d% D- T5 l6 @3 _+ j6 O. E
let result = exclaim(capitalize(doubleSay("hello")));
result //=> "Hello, hello!"
let result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
result //=> "Hello, hello!"
这只是对于单个参数的用法,其它的用法有兴趣的读者可以自行阅读提案,其中涉及到了特别多的内容,这大概也是导致推进阶段慢的原因吧。 d% g/ O0 S& Z1 n9 ]五、新的数据结构:Records & Tuples C+ F9 |2 ?% Z2 T& j7 f I) C) B
这个数据结构笔者觉得发布以后会特别有用,总共新增了两种数据结构,可以通过#来声明:6 Z( ^# @- a: r( L! B% w% ]" f
#{ x: 1, y: 2 }& M3 y% m, C7 ~2 M& V
#[1, 2, 3, 4]
/ G% @8 a: J' q6 Q& [' X; r5 t 这种数据结构是不可变的,类似 React 中为了做性能优化会引入的 immer 或者 immutable.js,其中的值只接受基本类型或者同是不可变的数据类型。" f/ t" P; [) m" p3 r1 _: p1 B. }