简体   繁体   中英

How to import functions javascript

i need to export my functions from index.js

And use it in app.js like myFunction() Dont var.myFunction() And whithout using

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

Because i need import them all

Assign all functions in index.js to module.exports

for example , below content in your index.js

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

to import them use

const myFunctions =require('index.js')

and then you can call myFunctions.add(2,3)

To learn more check module.exports in : https://learnjsx.com/category/2/posts/es6-moduleExports

Please learn more about the CommonJS module specification.

https://flaviocopes.com/commonjs/

Let's assume we have util.js .

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

Then as I know we can import everything like this.

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

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

Using ES6 syntax

import * as Util from 'utils.js'

Hope that answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM