1

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.

Andrew Cooper
  • 723
  • 2
  • 14
  • 28
  • 3
    `obj1[attr]` and `obj2[attr]` (bracket notation) - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors – Ian Jul 16 '14 at 07:20
  • Wow... that was fast guys. Thanks. I got stuck thinking in dot notation for some reason instead of remembering that you can treat objects like arrays. I'll select an answer below as soon as the timer allows me to do so. – Andrew Cooper Jul 16 '14 at 07:23
  • *"instead of remembering that you can treat objects like arrays"* that's not what you should be thinking either. Being able to access properties with bracket notation is a characteristic of objects, not of arrays. Arrays **are** objects. The fact that you see arrays only being accessed with bracket notation is due to the evaluation rules of dot notation. `foo.0` is simply invalid, hence you have to use `foo[0]`. Whether `foo` is an object or array doesn't matter. – Felix Kling Jul 16 '14 at 08:01

4 Answers4

2

You can do this, the syntax is just a little different in javascript.

function testTheory(attr) {
    obj1[attr] += 1;
    obj2[attr] += 2;
}

You use square braces around the property. (Side note: this also works with functions on an object.)

object[functionName](parameter); //same as object.functionName(parameters)
Caleb
  • 1,088
  • 7
  • 7
1

Try this:

var obj1 = {bob: 0, fred: 0};
var obj2 = {bob: 1, bill: 1};

function testTheory(attr) {
    obj1[attr] += 1;
    obj2[attr] += 2;
}

testTheory("bob");
Kiril
  • 2,935
  • 1
  • 33
  • 41
1

Short answer : Use obj[attr] instead of obj.attr

Explanation : attr in that case will be used by JavaScript to check for the actual attr attribute.

Let's say that we have an obj :

var obj = {name:"Bob", age:100, attr:"I am Bob"}

and two functions :

function getAttribute1(attr){ return obj.attr; }
function getAttribute2(attr){ return obj[attr]; }

getAttribute1("name") would return "I am Bob".

getAttribute2("name") would return ""Bob".

Zaenille
  • 1,384
  • 9
  • 16
1

The function should be like this

function testTheory(attr) {
    obj1[attr] += 1;
    obj2[attr] += 2;
}
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20