The Problem of Object Exports in Large JavaScript Codebases #489
-
import pythonmonkey as pm
js_code = """
function a(str) {
return b(str);
}
function b(str) {
return c(str);
}
function c(str) {
return str;
}
"""
hello = pm.eval(js_code + ";a")
print(hello("1751802131000")) For example, I have JavaScript code containing multiple interdependent functions, and I need to export several functions for use. Passing the entire JS code each time just to export a single function feels inelegant. Is there a more elegant implementation approach? |
Beta Was this translation helpful? Give feedback.
Answered by
zollqir
Jul 5, 2025
Replies: 1 comment 1 reply
-
There are many ways to do this. If you want to do this in a single file, the simplest would be to put those functions in a container, like a list or object. import pythonmonkey as pm
function_object = pm.eval("""() => {
function a(str) {
return b(str);
}
function b(str) {
return c(str);
}
function c(str) {
return str;
}
return {a, b, c};
}""")()
print(function_object.a("1751802131000"))
print(function_object.b("1751802131000"))
print(function_object.c("1751802131000")) Alternatively, if you're willing to use multiple files, you can make a CommonJS module that you export the functions from. definitions.js:function a(str) {
return b(str);
}
function b(str) {
return c(str);
}
function c(str) {
return str;
}
module.exports = { a, b, c } main.pyimport pythonmonkey as pm
a, b, c = pm.require("./definitions").values()
print(a("1751802131000"))
print(b("1751802131000"))
print(c("1751802131000")) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
zollqir
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are many ways to do this. If you want to do this in a single file, the simplest would be to put those functions in a container, like a list or object.
Alternatively, if you're willing to use multiple files, you can make a CommonJS module that you export the functions from.
definitions.js: