Import ES6 namespace: "this" behavior

What is the value of this in the imported function called from the import import ? (according to ECMA specification)

 // module.js export function fun() { return this; } // main.js import * as module from "./module.js"; let x = module.fun(); // What the value of x here? 

My guess would be: This is a module object, but did not find a clear answer to this in the specification. Does the normal behavior apply here or is there something special in ES6 modules for importing the namespace?

+5
source share
2 answers

Based on my reading of the specification, this will be an exotic module namespace object (which you tied to the module name in your example). In other words, an export bag. Therefore, in your example, I believe that you could call x.fun() in the following statement.

As far as I can see ...

+2
source

No, there is no particular behavior. Module namespaces can be exotic objects that delegate all calls to some internal elements and which are largely immutable, but they are still objects. Calling a method on them does not work otherwise than on any other object.

+3
source

All Articles