Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 namespace import: behavior of "this"

What's the value of this in an imported function, which is called from a namespace import? (according to the ECMA spec)

// module.js
export function fun() {
  return this;
}

// main.js
import * as module from "./module.js";

let x = module.fun(); // What's the value of x here?

My guess would be: It's the module object, but haven't found a clear answer to this in the spec. Does the normal behavior apply here or is there something special in ES6 modules for namespace imports?

like image 752
Tobias K. Avatar asked May 06 '16 09:05

Tobias K.


1 Answers

No, there is no special behaviour here. Module namespaces may be exotic objects which delegate all accesses to some internals and which are pretty much immutable, but they're still just objects. Method invocations on them don't work different than on any other object.

like image 180
Bergi Avatar answered Sep 22 '22 15:09

Bergi