如何导入函数 javascript

How to import functions javascript

我需要从 index.js

导出我的函数

并在 app.js 中使用它 像 我的函数() 不 var.myFunction() 并且不使用

{myFunction} = require("index.js")

因为我需要全部导入

Please learn more about the CommonJS module specification.

https://flaviocopes.com/commonjs/

假设我们有 util.js.

exports.one = () => {}
exports.two = () => {}
exports.three = () => {}

然后据我所知,我们可以像这样导入所有内容。

const Util = require('utils.js');

Util.one()
Util.two()
Util.three()

使用 ES6 语法

import * as Util from 'utils.js'

希望这能回答您的问题。

index.js中的所有函数分配给module.exports

例如,您 index.js

中的以下内容

module.exports= { t: x=> console.log ('this is '+x), add: (x,y) => x+y}

导入它们使用

const myFunctions =require('index.js')

然后你可以调用 myFunctions.add(2,3)

要了解更多信息,请查看 module.exports:https://learnjsx.com/category/2/posts/es6-moduleExports