本文介绍了AsyncFunction尚未定义,但MDN记录了它的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一篇文章 AsyncFunction-MDN上的JavaScript .它显示了以下代码段:

There is an article AsyncFunction - JavaScript on MDN. It shows the following snippet:

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

但是在Mozzila Firefox 55和Google Chrome中,都没有定义此构造函数:

Yet in both Mozzila Firefox 55 and Google Chrome, this constructor is not defined at all:

我发现(async function() {}).constructor确实是AsyncFunction,但是为什么不能在全局范围内看到它?

I found out that (async function() {}).constructor really is AsyncFunction, but why can't I see it in global scope?

推荐答案

如Mozilla文档中所述:请注意AsyncFunction不是全局对象."

As mentioned in the Mozilla docs "Note that AsyncFunction is not a global object."

因此,您不能像其他全局构造函数一样将其作为window对象的属性进行访问.必须通过查询async函数的实例来获得它:

Therefore you can't access it as a property of the window object like other global constructors. It must be obtained by interrogating an instance of an async function:

从文档中

const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;

这篇关于AsyncFunction尚未定义,但MDN记录了它的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:03