If you need to access it globally, you can modify the code like this:
function foo() {
console.log('Hello Word');
}
(window as any).foo = foo;
This way, the function will be accessible on the window
object. You can then call it using window.foo()
or simply foo()
since the window
object is global.
In JavaScript, variables and functions are private to the module unless explicitly exported. You can export a function like this:
export function foo() {
console.log('Hello Word');
}
Then, you can import the function in other modules like:
import {foo} from "foo";
foo();
Keep in mind that browsers do not natively support import
and export
syntax yet. Webpack bridges this gap by bundling all modules into a single file (e.g., "dist/bundle.js") with the required bootstrap code for the browser to understand.
It's important for JavaScript to maintain modularity to prevent conflicts between different scripts. This approach ensures a cleaner and more organized development environment.