javascript - Importing multiple functions from module without explicitly exporting object or named export - Stack Overflow

utilsmathlib.jsexport function add(x, y) {return x + y;}export function subtract(x, y) {return x - y

utils/mathlib.js

export function add(x, y) {
    return x + y;
}
export function subtract(x, y) {
    return x - y;
}

main.js

import add from "./../utils/mathlib"; //not working. but if I do default export like `export default function add(x, y)` it will work
import { add } from "./../utils/mathlib"; //working
import * as MathLib from "./../utils/mathlib"; //working

But I want to import all the functions available in the module with the same identifier without importing separately or through importing object. Something like the below,

import * from "./../utils/mathlib"

I should be able to use add, subtract function.

The reasoning behind this use case is, Whenever I add new functions in MathLib.js it should be available without modifying.(I took MathLib as sample use case only, in my real use case all the functions is necessary whenever I import the module).

utils/mathlib.js

export function add(x, y) {
    return x + y;
}
export function subtract(x, y) {
    return x - y;
}

main.js

import add from "./../utils/mathlib"; //not working. but if I do default export like `export default function add(x, y)` it will work
import { add } from "./../utils/mathlib"; //working
import * as MathLib from "./../utils/mathlib"; //working

But I want to import all the functions available in the module with the same identifier without importing separately or through importing object. Something like the below,

import * from "./../utils/mathlib"

I should be able to use add, subtract function.

The reasoning behind this use case is, Whenever I add new functions in MathLib.js it should be available without modifying.(I took MathLib as sample use case only, in my real use case all the functions is necessary whenever I import the module).

Share Improve this question asked Apr 9, 2018 at 7:22 Ember FreakEmber Freak 12.9k5 gold badges26 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You need to export your functions in an default object

export default {
    add(x, y) {
        return x + y;
    }
    subtract(x, y) {
        return x - y;
    }
}

You can't currently import into the global namespace without explicit named import/exports. It helps to prevent global namespace pollution and accidental overriding of variables in the global scope.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745146562a4613673.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信