Background
I usually code in python and am used to import libraries as follows:
import numpy as np
and then access the library as:
a = np.array([1,2,3])
However in javascript I can't get things to work this way. I am able to import a library through a url as follows:
<script src="//cdn.shopify.com/s/files/1/0561/6726/8513/t/1/assets/hello.js?v=6940067293431132177" crossorigin="anonymous"></script>
say_hi(); //say_hi is defined in hello.js
This works, but it is very confusing. I now have a bunch of functions that I can access directly but this pollutes my namespace. On top of that it isn't very clear from which library the functions are coming.
Question
Is there a way to access my 'hello' library through usage of an URL as follows:
hello.say_hi();
If so how do you do it?
Research
I know that you can use the 'export' and 'import' functions as follows
import * as hello from './hello.js';
with in hello.js an export defined as:
export {say_hi};
However I need to import it from a URL and I don't know how this would work with a URL.
I also found the following post and thus tried:
<script src="//cdn.shopify.com/s/files/1/0561/6726/8513/t/1/assets/hello.js?v=17694146303704268990"></script>
const hello = window.hello;
hello.say_hi()
But in this case hello is undefined...
I also tried out this post (answer of Rahul Srivastava):
var hello = document.createElement('script');
hello.setAttribute('src','//cdn.shopify.com/s/files/1/0561/6726/8513/t/1/assets/hello.js?v=17694146303704268990');
document.head.appendChild(hello);
hello.say_hi();
But then again hello.say_hi() is not recognized.
If this question is already answered elsewhere please do refer me, I can't find it.