admin管理员组文章数量:1326328
How do I assign a namespace using ES6 modules? I'd like to do with for example jQuery does, where the namespace is $
but doing it the intended ES6 way. All my modules are structured in separate files which export the classes/functions/whatever as default (e.g. export default class Pikachu
). How do I import it into another (main) file so that a user can access this class using e.g. Namespace.Pikachu
?
I have e to understand that it might have to do with named exports but I'm not quite totally sure how. Any help please?
How do I assign a namespace using ES6 modules? I'd like to do with for example jQuery does, where the namespace is $
but doing it the intended ES6 way. All my modules are structured in separate files which export the classes/functions/whatever as default (e.g. export default class Pikachu
). How do I import it into another (main) file so that a user can access this class using e.g. Namespace.Pikachu
?
I have e to understand that it might have to do with named exports but I'm not quite totally sure how. Any help please?
Share Improve this question edited Feb 8, 2016 at 2:10 AKG asked Feb 8, 2016 at 2:04 AKGAKG 3,3166 gold badges30 silver badges38 bronze badges 3- 2 See second example. – rgajrawala Commented Feb 8, 2016 at 2:12
-
You just export an object with a
Pikachu
property, as always. But why would you do that, instead of letting the userimport … from '…/pikachu'
whatever and in whichever way he chooses? – Bergi Commented Feb 8, 2016 at 2:12 - @usandfriends thanks! i must have accidentally skipped it over when reading that article! – AKG Commented Feb 8, 2016 at 2:25
2 Answers
Reset to default 5If you use modules, you don't need namespaces.
The point of namespaces is to prevent conflicts between different files that define the same names.
Modules eliminate this problem entirely by letting the callsite choose a name to give each module it needs.
You just export a simple object with the things you want, and other files can import that to any name they choose.
It's an old question with an accepted answer, but I feel that one should mention that module namespaces are nevertheless supported to some extent. As was already noted by the ment of @rgajrawala , an import of all exported variables from a module via "*" always creates such a namespace. (see here on MDN)
import * as Namespace from "./myModule.js";
console.log(Namespace.default); // in the OP, Pikachu is exported as default
//console.log(Namespace.Pikachu); // if Pikachu wasn't exported as default
Similarly, dynamic imports always return a Promise for a module-namespace:
const Namespace = await import("./myModule.js");
console.log(Namespace.default);
//console.log(Namespace.Pikachu);
For more details on what kind of object such a namespace is, see this SO answer
A third alternative (in general not preferable, see ments to this answer) is to create a namespace already in the module and make it the default export:
// within "./myModule.js"
export default {Pikachu, otherVariable, ...};
// within the other module
import Namespace from "./myModule.js";
console.log(Namespace.Pikachu);
本文标签: javascriptnamespace with ES6 modulesStack Overflow
版权声明:本文标题:javascript - namespace with ES6 modules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742202820a2432274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论