Fork me on GitHub
#nbb
<
2024-03-01
>
Duck Nebuchadnezzar19:03:51

If I have a file named backup.js with the contents const data = [ { "foo": "bar" } ], what is the best way to eval and read that data without needing to modify the file?

borkdude20:03:27

Read the file and use Js/eval?

Duck Nebuchadnezzar20:03:37

ok. That's about what I figured. Wanted to make sure there wasn't something else I was missing

Duck Nebuchadnezzar20:03:14

So, it seems like doing that, I can read it if it's var data = but not const data =

borkdude20:03:45

$ nbb -e '(js/eval "const x = 1; x")'
1

👍 1
Duck Nebuchadnezzar20:03:53

I was trying to do it with js/data which only worked for var but not const or let

borkdude20:03:40

never heard of js/data

borkdude20:03:57

$ nbb -e '(prn js/data)'
nil

Duck Nebuchadnezzar20:03:07

data was the var that was defined inside the eval-ed file. I was trying to refer to it using js/ but your way works.

borkdude20:03:16

another way could be:

$ nbb -e '(def f (js/Function. "const x = 1; return x;")) (f)'
1

👍 1
borkdude20:03:26

but you still have to add something to the source