re: Why will my code not keep the variables in the arrays for sub-values? VIEW POST


Hi Calin,

Yes, as you hinted at, every time you call the function _, the variables JRVars and JRVals get reset to undefined. To solve this you can take advantage of the concept of closure by defining an outer function that sets up your desired variables and then returns another function (which will retain access to those previously defined variables) that handles all the relative logic.

In other words, you can set up an initializing function that upon invocation initializes your variables and gives you back a utility function you can then use to perform all the work. Something like this, for example:

If you want to read more on how scope and closure works in JS, check this out:


Thank you for your contribution to what will sometime in the near future be JReply JavaScript library! but I had a question, could I use a throw and catch statement to bypass this? i.e.

throw {
    if(JRVars[0] == undefined && JRVals[0] == undefined) {
} catch {
    var JRVars;
    var JRVals;

(Also is there a way to get it in scope of the function? Thanks), or does this method not work? Thanks much for your contribution!


Hi again,

I don't think using a try / catch block here would be very helpful, but then again I'm not as familiar with the problem you're trying to solve. At any rate, if you wanted to go that route, you could do so by following a pattern like the following:

Hope that helps and I wish you success with your library!

Thanks again for helping me, I can't wait till its ready for full realese!
Its similar to JQuery in many aspects but its still unique!

code of conduct - report abuse