admin管理员组文章数量:1193970
With JavaScript what's the shortest way to import a named export, rename it, and export it again?
This code works but it feels more verbose than it should be
import { mock as myFunctionMock } from 'context/myFunction';
export const myFunction = myFunctionMock;
With JavaScript what's the shortest way to import a named export, rename it, and export it again?
This code works but it feels more verbose than it should be
import { mock as myFunctionMock } from 'context/myFunction';
export const myFunction = myFunctionMock;
Share
Improve this question
asked Mar 25, 2019 at 14:02
EvanssEvanss
23.5k99 gold badges321 silver badges551 bronze badges
7
|
Show 2 more comments
3 Answers
Reset to default 19You can combine the import and export like so:
export { mock as myFunctionMock } from 'context/myFunction';
See MDN Docs
Note that you won't actually be able to use myFunctionMock
within your code file since you haven't imported it. Neither mock
nor myFunctionMock
will be defined within this module.
This is a useful shorthand when you're building a library that will be used by other modules or by your end-user.
For example, if you had a utils
library that you wanted to export, but you wanted to organize your util functions across several smaller files, such as stringUtils
, objectUtils
, dataUtils
, etc, you can export the contents of those modules within your utils
module to create a single, monolithic access point:
stringUtils.js
export function toLower(){}
export function toUpper(){}
objectUtils.js
export function propertyMap(){}
utils.js
export {
toLower as stringToLower,
toUpper as stringToUpper,
} from "stringUtils.js";
export {
propertyMap as objectPropertyMap
} from "objectUtils.js";
I wouldn't generally recommend this approach for internal code as it can make your dependency trees a bit wonky in some cases. It can, however, be extremely useful in situations where you want to import from a common interface but the implementation is dependent on the build (prod vs dev, web vs node, etc)
import { mock as myFunction } from 'context/myFunction';
export { myFunction };
in your original exporter, do:
module.exports = { mock: function () {...}}
When importing, do:
const myFunctionMock = require('file path of exporter');
then to reexport in the same file:
module.exports = {renamedMock: myFunctionMock};
Now any changes to mock
will propagate to the other modules where it's referenced (side note, this is node.js in a nutshell).
本文标签: importrenameand export a function in JavaScriptStack Overflow
版权声明:本文标题:Import, rename, and export a function in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738438942a2086844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
myFunctions
toexternalLibrary
andcalculateFoo
tocalculateBar
and then every time you usecalculateFoo
in your actual code you have to change it tocalculateBar
? Sure, you can do that. I don't think it's easier than NOT doing all of that and changing a single line. – VLAZ Commented Mar 25, 2019 at 14:12