在JavaScript中,如果你想要导出多个值,你可以使用ES6的export语法。如果你想要导出多个具名的值,你可以这样做:
// mathUtils.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
export const multiply = (a, b) => a * b;
export const divide = (a, b) => a / b;
如果你想要在另一个文件中导入这些值,你可以使用import语法,并且使用花括号{}来指定你想要导入的具名导出:
// main.js
import { add, subtract, multiply, divide } from './mathUtils.js';
console.log(add(5, 3)); // 8
console.log(subtract(5, 3)); // 2
console.log(multiply(5, 3)); // 15
console.log(divide(10, 2)); // 5
如果你想要一次性导出模块中的所有值,你可以使用export * from语法:
// mathUtils.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;
export { add, subtract, multiply, divide };
然后在另一个文件中导入所有这些值:
// main.js
import * as mathUtils from './mathUtils.js';
console.log(mathUtils.add(5, 3)); // 8
console.log(mathUtils.subtract(5, 3)); // 2
console.log(mathUtils.multiply(5, 3)); // 15
console.log(mathUtils.divide(10, 2)); // 5
以上就是在JavaScript中导出和导入多个值的基本方法。