V8 v9.1 版本新特性:顶层 await
作者:互联网
V8 release v9.1 04 May 2021 release
V8 release v9.1
Chrome V8
于 5 月 4 号发布 v9.1
版本,带来几个新特性
一、Top-level await
(顶层 await
)
Top-level await is enabled by default in V8 starting with v9.1 and is available without --harmony-top-level-await.
一句话就是:我们可以在模块顶级中使用 await
,不需要在额外的加入 async
so happy
,这样的话就能避免一些冗余的代码
尝试一下…(请把 Chrome
浏览器升级到最新版)
1. 在页面中写入如下代码,写一个 sleep
延迟函数,并打开页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
const sleep = (time = 500) => {
console.log('sleep 执行')
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, time)
})
}
await sleep()
</script>
居然报错了。。。
await is only valid in async functions and the top level bodies of modules
await
仅在异步函数和模块的顶级主体中有效
so
,我们改一下,把 script
改成 module
,再次打开页面。
<script type="module">
const sleep = (time = 500) => {
console.log('sleep 执行')
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, time)
})
}
await sleep()
</script>
执行成功。
tips:
Top-level await only works at the top level of modules. There is no support for classic scripts or non-async functions.
顶层 await
仅适用于模块的顶层。 不支持经典脚本或非异步函数。
二、Private brand checks a.k.a. #foo in obj
The private brands check syntax is enabled by default in v9.1 without requiring --harmony-private-brand-checks. This feature extends the in operator to also work with private fields’ #-names.
简单说拓展了 in
操作符对于私有属性的检查
1. 示例
class A {
static test(obj) {
console.log(#foo in obj);
}
#foo = 0;
}
A.test(new A()); // true
A.test({}); // false
class B {
#foo = 0;
}
A.test(new B()); // false; it's not the same #foo
三、简短的内置调用
参考
标签:level,v9.1,await,sleep,V8,foo 来源: https://blog.csdn.net/guoqiankunmiss/article/details/117651343