0

What is the simplest and most generic function wrapper in javascript? I have a lot of variables and objects I need to put a function wrapper around. I'm currently using:

x = {a:function () {return aValue; }, b:function () {return bValue; }, c:'cString' };

I have many such variables and property function wrappers and would like to do this with less coding.

Is there something of the form:

x = {a:wrap(aValue), b:wrap(bValue), c:'cString'}

... or similar (or even a loop through all properties) that would accomplish putting a function wrapper around these so that I can use a get function later to evaluate the property values?

My get function looks like this:

function get(e) {return (typeof e === 'function') ? e () : e; }

Thanks

C G
  • 25
  • 1
  • 6
  • 1
    I've never felt the need to wrap my properties. Why are you? – John Dvorak Sep 13 '13 at 00:23
  • 1
    ^^ Yeah, looks like an XY problem... what are you trying to do exactly? Can't you just store everything in an object and lookup the properties there? – elclanrs Sep 13 '13 at 00:26
  • Because I need to evaluate them later (lazily), since processing of prior array elements and or objects can affect state such that subsequent properties and variables need to be determined based on current state (at the time they are evaluated). – C G Sep 13 '13 at 00:26
  • see link for related question and solution: http://stackoverflow.com/questions/18744986/how-to-do-lazy-evaluation-of-an-array-being-processed-by-a-for-loop – C G Sep 13 '13 at 00:27
  • Looking at your other question, why can't you just check the index and assign `x` only if it matches the position you want? Not sure I understand your problem... – elclanrs Sep 13 '13 at 00:32
  • 1
    Since JS doesn't have macros, `wrap(aValue)` will evaluate `aValue` at that time, you can't get lazy evaluation without writing an explicit function. – Barmar Sep 13 '13 at 00:34
  • the example was simple. x needs to be determined after various other things have happened and involve other variables as well (not in that particular array). I need to determine x at it's proper time. I have it working fine, it's just the function wrappers seem like a lot of code that is repetitive. If there is a simpler generic way to bury these variables in their annonymous function, then my code would look cleaner (and simpler.) – C G Sep 13 '13 at 00:36
  • thanks, Barmar... that is what I suspected, but in case any javascript experts know of a way that I wasn't aware of, I thought I would ask. Functions can get very intense in javascript so I wasn't sure. – C G Sep 13 '13 at 00:37

0 Answers0