I've got the following code in Javascript...
var obj1 = {bob: 0, fred: 0};
var obj2 = {bob: 1, bill: 1};
function testTheory(attr) {
obj1.attr += 1;
obj2.attr += 2;
}
testTheory("bob");
I've got very similar code in PHP that works fine. The testTheory function should take the string "bob" and attempt to use it like a property on obj1 and obj2. Is there a way to accomplish this in Javascript? The purpose of the question is that I have multiple objects that have similar property lists and I need to total some of the properties. Rather than have separate functions that total the "bob" property and then the "bill" property, it'd be nice to simply pass the name of the property I want to total and have it add those up. I got it to work in PHP easily enough but am struggling with Javascript.