Suppose I have two functions a
and b
that I want to keep within a certain functional scope. Since they share some common code, I decide to extract this shared functionality into another method named support
.
support
should be accessible by both a
and b
, but I prefer it not to be visible to other methods within the same scope. Is there a way to achieve this?
// This is the scope; could be global or inside another function
function a() {
let res = support()
res.name = "a"
return res
}
function b() {
let res = support()
res.name = "b"
return res
}
function support() {
return {"foo": "bar", "name": "support"}
}
I had an idea while writing this, which I will share as an answer. However, it's not exactly what I'm looking for since a
and b
end up being function expressions instead of behaving like regular function declarations (if my terminology is correct).