在 Javascript 中,(动态)导入的 ES6-Module 实例的 type/class 是什么?
In Javascript, what is the type/class of a (dynamically) imported ES6-Module instance?
动态加载模块后,即在相应的承诺已解决后,可以对结果使用函数或 lambda 表达式。惊讶的发现module-instance(由promise提供)没有原型:
import("./module.js").then(x => {
console.log(typeof x); // yields 'object' as expected
console.log(x instanceof Object); // yields false !!
console.log(x.__proto__); // yields undefined !!
console.log(Object.getPrototypeOf(x)); // yields null !!
// but one can certainly access exported variables / functions
x.exportedFunction();
})
显然,尽管 'object',x
似乎不是 Object
实例,而且似乎根本没有任何原型。我什至不知道这是可能的。 Firefox/Chrome 和 Safari 都给出了相同的输出(我在代码注释中拼写的那个)。有解释吗?这是在任何地方指定的吗?这个模块是什么类型的对象x
?
注意:我不熟悉打字稿,所以我不确定我的问题是否可以被认为是this one
的重复
编辑:我刚刚意识到,通过 import * as x from "./module.js"
静态导入模块到命名空间或 module object 中获得了相同的怪异伪对象。尽管 MDN 提到了这些对象,但并未详细说明它们是什么。
有一整套所谓的exotic objects, which deviate from ordinary objects. The type of an imported ES6-Module instance is one of these, the Module Namespace Exotic Object:
An object is a module namespace exotic object if its [[GetPrototypeOf]], [[SetPrototypeOf]], [[IsExtensible]], [[PreventExtensions]], [[GetOwnProperty]], [[DefineOwnProperty]], [[HasProperty]], [[Get]], [[Set]], [[Delete]], and [[OwnPropertyKeys]] internal methods use the definitions in this section, and its other essential internal methods use the definitions found in 10.1. These methods are installed by ModuleNamespaceCreate.
为什么 x <a href="https://tc39.es/ecma262/#sec-instanceofoperator" rel="nofollow noreferrer">instanceof</a> Object
, x.<a href="https://tc39.es/ecma262/#sec-get-object.prototype.__proto__" rel="nofollow noreferrer">__proto__</a>
, Object.<a href="https://tc39.es/ecma262/#sec-instanceofoperator" rel="nofollow noreferrer">getPrototypeOf</a>(x)
表现得如此奇怪?我们看一下这里涉及到的对象内部方法:
GetPrototypeOf
The [[GetPrototypeOf]] internal method of a module namespace exotic
object takes no arguments and returns a normal completion containing
null. It performs the following steps when called:
1. Return null.
这些对象是如何创建的?有一个 special method:
ModuleNamespaceCreate(模块,导出)
The abstract operation ModuleNamespaceCreate takes arguments module (a
Module Record) and exports (a List of Strings) and returns a module
namespace exotic object. It is used to specify the creation of new
module namespace exotic objects. It performs the following steps when
called:
1. Assert: module.[[Namespace]] is empty.
2. Let internalSlotsList be the internal slots listed in Table 35.
3. Let M be MakeBasicObject(internalSlotsList).
4. Set M's essential internal methods to the definitions specified in 10.4.6.
5. Set M.[[Module]] to module.
6. Let sortedExports be a List whose elements are the elements of exports ordered as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn.
7. Set M.[[Exports]] to sortedExports.
8. Create own properties of M corresponding to the definitions in 28.3.
9. Set module.[[Namespace]] to M.
10. Return M.
动态加载模块后,即在相应的承诺已解决后,可以对结果使用函数或 lambda 表达式。惊讶的发现module-instance(由promise提供)没有原型:
import("./module.js").then(x => {
console.log(typeof x); // yields 'object' as expected
console.log(x instanceof Object); // yields false !!
console.log(x.__proto__); // yields undefined !!
console.log(Object.getPrototypeOf(x)); // yields null !!
// but one can certainly access exported variables / functions
x.exportedFunction();
})
显然,尽管 'object',x
似乎不是 Object
实例,而且似乎根本没有任何原型。我什至不知道这是可能的。 Firefox/Chrome 和 Safari 都给出了相同的输出(我在代码注释中拼写的那个)。有解释吗?这是在任何地方指定的吗?这个模块是什么类型的对象x
?
注意:我不熟悉打字稿,所以我不确定我的问题是否可以被认为是this one
的重复编辑:我刚刚意识到,通过 import * as x from "./module.js"
静态导入模块到命名空间或 module object 中获得了相同的怪异伪对象。尽管 MDN 提到了这些对象,但并未详细说明它们是什么。
有一整套所谓的exotic objects, which deviate from ordinary objects. The type of an imported ES6-Module instance is one of these, the Module Namespace Exotic Object:
An object is a module namespace exotic object if its [[GetPrototypeOf]], [[SetPrototypeOf]], [[IsExtensible]], [[PreventExtensions]], [[GetOwnProperty]], [[DefineOwnProperty]], [[HasProperty]], [[Get]], [[Set]], [[Delete]], and [[OwnPropertyKeys]] internal methods use the definitions in this section, and its other essential internal methods use the definitions found in 10.1. These methods are installed by ModuleNamespaceCreate.
为什么 x <a href="https://tc39.es/ecma262/#sec-instanceofoperator" rel="nofollow noreferrer">instanceof</a> Object
, x.<a href="https://tc39.es/ecma262/#sec-get-object.prototype.__proto__" rel="nofollow noreferrer">__proto__</a>
, Object.<a href="https://tc39.es/ecma262/#sec-instanceofoperator" rel="nofollow noreferrer">getPrototypeOf</a>(x)
表现得如此奇怪?我们看一下这里涉及到的对象内部方法:
GetPrototypeOf
The [[GetPrototypeOf]] internal method of a module namespace exotic object takes no arguments and returns a normal completion containing null. It performs the following steps when called:
1. Return null.
这些对象是如何创建的?有一个 special method:
ModuleNamespaceCreate(模块,导出)
The abstract operation ModuleNamespaceCreate takes arguments module (a Module Record) and exports (a List of Strings) and returns a module namespace exotic object. It is used to specify the creation of new module namespace exotic objects. It performs the following steps when called:
1. Assert: module.[[Namespace]] is empty. 2. Let internalSlotsList be the internal slots listed in Table 35. 3. Let M be MakeBasicObject(internalSlotsList). 4. Set M's essential internal methods to the definitions specified in 10.4.6. 5. Set M.[[Module]] to module. 6. Let sortedExports be a List whose elements are the elements of exports ordered as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn. 7. Set M.[[Exports]] to sortedExports. 8. Create own properties of M corresponding to the definitions in 28.3. 9. Set module.[[Namespace]] to M. 10. Return M.