logo

How to Use Functions Outside a JavaScript Module 📂Programing

How to Use Functions Outside a JavaScript Module

Overview

In JavaScript, functions within a module cannot be used externally. In the web environment, modules are defined with the <script type="module"> tag, and while the module itself can be exported through export, to use a function defined within the module, it must be passed to the window DOM and redefined.

Code

<script type="module">
    function _f(){
        return ...
    }
    window.f = _f;
<script>
  • By using window.f = _f, the function _f defined inside the module is exported as a function of the window interface itself.