问题描述
有一篇文章 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记录了它的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!