I have arrays within an array that are created and passed as arguments in a method of an object like this:
myObject.method([
{
id: "mystring",
myfun: function(g) {
return 'Value: ' + g.value + g.rows[0]['value'];
},
a: "mysecondstring"
},
id: "mystring_a",
myfun: function(g) {
return 'Value: ' + g.value + g.rows[0]['value'];
},
a: "mysecondstring_a"
}]);
This setup works well throughout my code. However, I now need to change the myfun: function(g)...
at runtime, which means I have to declare my associative array before calling myObject.method()
. Here is what I attempted:
myfun: new Function("g", "return 'Value: ' + g.value + " + my_dynamic_variable)
That didn't work, so I tried:
var fn = new Function("g", "return 'Value: ' + g.value + " + my_dynamic_variable)
along with
myfun: fn(g)
and also
myfun: function(g) { return fn }
and
var fn = function(g) { return eval('Value: ' + g.value + my_dynamic_variable) }
with
myfun: fn
Every attempt I made resulted in null
for g
once the myfun
code was executed. It seems like variable g
needs to be bound within the scope of myObject
, and can't be defined outside of that scope, but I'm not entirely sure.
Does anyone know a way to accomplish this? Am I on the right track or missing something here? Thank you!