how to include javascript files in a javascript file?

Asked

Viewed 56 times

-2

so I’m needing to include a javascript file inside a javascript file, but I can’t do that

ex: inserir a descrição da imagem aqui

include.js file in archiv2.js

  • 1

    If it’s just include, it’s okay to copy all the contents of arquivo.js and paste at the top of the contents of arquivo2.js. Now, if you need to use a function created in the first file, edit your question and better describe what you need to do.

2 answers

1

Hello, it is not possible to do this type of include in JS.

But you can use import and export to reuse code within another file. However, in an Ecmascript 6 feature not yet implemented in all browsers you need to use transpilers like Babel to work

Ex.:

given the file Function.js

function returnName(name) {
 console.log('Olá ' + name)  
}
export default returnName

and index.js file

import returnName from './function'

returnName('Salatiel)
// Olá, Salatiel

See the documentation of import and export

1

To use import and export you have to be using the Babel lib, then use module.Exports module.Exports is a global variable in the JS can put its functions, classes and even variables, to import a module into that model you have to use require() const myFunc = require(2ª wayDoArch')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.