From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/asyncfunction/index.html | 121 +++++++++++++++++++++ .../asyncfunction/prototype/index.html | 57 ++++++++++ 2 files changed, 178 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/asyncfunction/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/asyncfunction') diff --git a/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/index.html b/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/index.html new file mode 100644 index 0000000000..e24d75c33f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/index.html @@ -0,0 +1,121 @@ +--- +title: AsyncFunction +slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction +tags: + - JavaScript + - Reference + - 实验性内容 + - 构造函数 +translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction +--- +
{{JSRef}}
+ +
 
+ +

AsyncFunction 构造函数用来创建新的 {{jsxref("Statements/async_function", "异步函数")}} 对象,JavaScript 中每个异步函数都是  AsyncFunction 的对象。

+ +

注意,AsyncFunction 并不是一个全局对象,需要通过下面的方法来获取:

+ +
Object.getPrototypeOf(async function(){}).constructor
+
+ +

语法

+ +
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
+ +

参数

+ +
+
arg1, arg2, ... argN
+
函数的参数名,它们是符合 JavaScript 标示符规范的一个或多个用逗号隔开的字符串。例如 xtheValue、或 a,b
+
functionBody
+
一段字符串形式的 JavaScript 语句,这些语句组成了新函数的定义。
+
+ +

描述

+ +

执行 AsyncFunction 构造函数的时候,会创建一个 {{jsxref("Statements/async_function", "异步函数")}} 对象。但是这种方式不如先用 {{jsxref("Operators/async_function", "异步函数表达式")}} 定义一个异步函数,然后再调用其来创建 {{jsxref("Statements/async_function", "异步函数")}} 对象来的高效,因为第二种方式中异步函数是与其他代码一起被解释器解析的,而第一种方式的函数体是单独解析的。

+ +

传递给 AsyncFunction 构造函数的所有参数,都会成为新函数中的变量,变量的名称和定义顺序与各参数相同。 

+ +
+

注意:使用 AsyncFunction 构造函数创建的{{jsxref("Statements/async_function", "异步函数")}} 并不会在当前上下文中创建闭包,其作用域始终是全局的。因此运行的时候只能访问它们自己的本地变量和全局变量,但不能访问构造函数被调用的那个作用域中的变量。这是它与 {{jsxref("Global_Objects/eval", "eval")}} 不同的地方。

+
+ +

调用 AsyncFunction 构造函数时可以省略 new,其效果是一样的。

+ +

属性

+ +
+
AsyncFunction.length
+
AsyncFunction 构造函数的 length 属性,值为 1。
+
{{jsxref("AsyncFunction.prototype")}}
+
通过原型对象可以为所有异步函数对象定义额外的属性。
+
+ +

AsyncFunction 原型对象

+ +

属性

+ +
{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype', '属性')}}
+ +

AsyncFunction 实例

+ +

AsyncFunction 实例继承了 {{jsxref("AsyncFunction.prototype")}} 的方法和属性。和所有构造函数一样,修改 AsyncFunction 构造函数的原型对象会同时对所有 AsyncFunction 实例上生效。

+ +

示例

+ +

通过 AsyncFunction 构造器创建一个异步函数

+ +
function resolveAfter2Seconds(x) {
+  return new Promise(resolve => {
+    setTimeout(() => {
+      resolve(x);
+    }, 2000);
+  });
+}
+
+var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
+var a = new AsyncFunction('a',
+                          'b',
+                          'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);');
+a(10, 20).then(v => {
+  console.log(v); // 4 秒后打印 30
+});
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-objects', 'AsyncFunction object')}}{{Spec2('ESDraft')}}ES2017 中的初始定义
+ +

浏览器兼容性

+ + + +
{{Compat("javascript.builtins.AsyncFunction")}}
+ +

参见

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html new file mode 100644 index 0000000000..9a8678680a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/asyncfunction/prototype/index.html @@ -0,0 +1,57 @@ +--- +title: AsyncFunction.prototype +slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/AsyncFunction/prototype +--- +
{{JSRef}}
+ +

AsyncFunction.prototype 属性表示 {{jsxref("AsyncFunction")}} 的原型对象。

+ +

描述

+ +

{{jsxref("AsyncFunction")}} 对象继承自 AsyncFunction.prototypeAsyncFunction.prototype 不能被修改。

+ +

属性

+ +
+
AsyncFunction.constructor
+
默认值为 {{jsxref("AsyncFunction")}}。
+
AsyncFunction.prototype[@@toStringTag]
+
返回 "AsyncFunction"。
+
+ +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-async-function-constructor-prototype', 'AsyncFunction.prototype')}}{{Spec2('ESDraft')}}最初定义在ES2017.
+ +

兼容性

+ +
+
+ + +

{{Compat("javascript.builtins.AsyncFunction.prototype")}}

+
+
+ +

参见

+ + -- cgit v1.2.3-54-g00ecf