In addition to my earlier answer, I propose a possible solution to solve the a variable access issue for you.
In your answer, you gave this example:
const finalValue = q(w(e(r(t(y(u(i(o(p(a(s(d(f(g(h(j(k)))))))))))))))))))
Suppose the value of a(s(d(f(g(h(j(k))))))) should be calculated once and reused in functions q, w, e, r, t, y, u, i, o, and p.
Then you would get something like this:
const x = a(s(d(f(g(h(j(k)))))))
const finalValue = q(w(e(r(t(y(u(i(o(p(x))))))))))))
Now suppose you want to avoid that variable x is accessed again:
const x = a(s(d(f(g(h(j(k)))))))
const finalValue = q(w(e(r(t(y(u(i(o(p(x))))))))))))
const trySomething = x // This should fail, because x is already accessed for calculating finalValue!
In that case, I would create a separate function, say calculate, to perform the complex calculation (bookmark 1):
function calculate(k) {
const x = a(s(d(f(g(h(j(k)))))))
return q(w(e(r(t(y(u(i(o(p(x))))))))))))
}
const finalValue = calculate(k)
As you can see here, variable x is now only accessible within function calculate for the purpose of the complex calculation.
However, there is no way to avoid calling the calculate function multiple times. Each time, variable x will be re-calculated as well. If you want to avoid that, you could use a variable to check if function calculate is already executed (bookmark 2):
let calculated = false;
function calculate(k) {
if (calculated) {
throw new Error('Do not call me again!')
}
calculated = true
const x = a(s(d(f(g(h(j(k)))))))
return q(w(e(r(t(y(u(i(o(p(x))))))))))))
}
const finalValue = calculate(k)
const finalValue2 = calculate(k) //Will throw error
But it might be possible to manipulate the calculated variable here. Using first-class functions (functions that return functions) and closures might help you in that case (bookmark 3):
function generate_calculate_function() {
let calculated = false;
return function(k) {
if (calculated) {
throw new Error('Do not call me again!')
}
calculated = true
const x = a(s(d(f(g(h(j(k)))))))
return q(w(e(r(t(y(u(i(o(p(x))))))))))))
}
}
const calculate = generate_calculate_function();
const finalValue = calculate(k)
const finalValue2 = calculate(k) //Will throw error
The generated calculate function can now be called only once. And there is no way to manipulate the calculated variable from the outside.
However, there is now no restriction on generating a second calculate function and use that instead:
const calculate = generate_calculate_function();
const finalValue = calculate(k)
const calculate2 = generate_calculate_function();
const finalValue2 = calculate2(k) // Fine
So in the end, there will be no way to avoid calculating the value of variable x inside the calculate function multiple times.
But in my opinion, there should be no need to avoid multiple calculations at all. It should technically not be an issue to call function calculate multiple times. Or does the function have side effects and should your program thus be used only once and then never again? In that case, I would simply add some initial logic that somehow checks if your program has already been run in the past and only continue if that is not the case. Or better yet: I would try to separate the "pure" (repeatable) logic from the "unpure" (side-effects) logic and put any necessary run-once checks in that "unpure" logic, so that it will be skipped in subsequent calls. (This is an example of what I meant in my comment for you to consider rethinking your code structure/design.)
So I personally would just go with the code under bookmark 1 above. As I see it, the value of x is only used within the scope that needs it (the logic of function calculate) and it cannot be falsely used anywhere else.
NB:
My solutions also use run-time checks. As far as I know, there is no way to let the compiler check if (and disallow that) a variable or function is being accessed multiple times.