I have associative arrays within an array that is created and passed as an argument within an object method like so:
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"
}]);
I have plenty of instances of this throughout my code and this works fine. The value of g is not declared beforehand. Now I want to change the myfun: function(g)... at run time, which requires that I declare my associative array before calling myObject.method(). Here is what I have tried:
myfun: new Function("g", "return 'Value: ' + g.value + " + my_dynamic_variable)
no good, then tried:
var fn = new Function("g", "return 'Value: ' + g.value + " + my_dynamic_variable)
with
myfun: fn(g)
and this
myfun: function(g) { return fn }
and this
var fn = function(g) { return eval('Value: ' + g.value + my_dynamic_variable) }
with
myfun: fn
Everything I have tried turns up null for g once the myfun code is executed. I'm guessing that variable g must be bound within the scope of myObject, and that I cannot define it outside of the myObject scope, but I am not certain.
Anyone know of a way to do this? Am I missing the mark here? Thanks!
gis evaluated when the function is executed, it's a parameter.var f = function(g){return 'Value: '+ g.value + g.rows[0].value}, thenmyfun: f