简体   繁体   中英

Importing functions from other file

I have a two Javascript files both having some functions. I want to access functions from one file into another. How can I do it without webpack or any other module bundler.

In the file (methods.js) you have the method you want to export:

    export function Example() {
        return “value”
    }

Then in the file where you will import the method:

    import Example from ‘./methods.js’;

Javascript function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.

A function cannot be called unless it is in the same or greater scope, then the one trying to call it.

Suppose you declare a function function1() in One.js, and then in Two.js you can just have function1();

One.js

function function1 () {
    alert('Test');
}

Two.js

function1 ();

Test.html

<script type="text/javascript" src="One.js"></script>
<script type="text/javascript" src="Two.js"></script>

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