Working with a class (or sharing scripts with Obsidian)

I am trying to do something beyond my javascript knowledge, so apologies if this question sounds garbled!

In Obsidian, there’s a way to reuse javascript across the app. It requires creating a file defining a class. I have the following class:

class hArray {
		this.hray = ['thing1','thing3', 'thing3']

I would like to reuse this array in Drafts. How can I access the array in javascript? I duplicated the file to Drafts’ scripts folder and tried a few variations of the following, but no joy (returns “undefined”):

let h = hArray

Bonus points if there’s a way for me to use “require” to grab this file from Obsidian’s iCloud Drive folder, without having to duplicate it somehow between that folder and Drafts’ scripts folder.

First, with what you have, you would need to instantiate an instance of the class hArray, so the line in Drafts would look like let h = new hArray(). Believe that should work.

Second, require in Drafts works just as if the code from the loaded file was inline in the other script. So, you don’t need the class construct, etc, you can just define a global variable. So your hArray.js file could just be:

const hArray = ['thing1','thing3', 'thing3']

And the hArray would be available to code after the require.

Lastly, if you want to load code from outside the Drafts /Library/Scripts folder, you would have to read the content of the file into a string and call eval on the string, rather than using require. The result would be the same. Something like:

let bookmark = Bookmark.findOrCreate("My-Folder")
let fm = FileManager.createForBookmark(bookmark)

let script = fm.readString("/hArray.js")

great, thanks. The h = new hArray() code works when I duplicate the script into the Drafts scripts folder. However, the eval method doesn’t seem to work–I can read the script in with the file manager and see it in the console, but the eval doesn’t allow me to instantiate the class or work with the array.

Unfortunately, I have to leave the script as a class because that’s how Obsidian needs to work with it, so for interoperation, I need to leave it as is.