3

For example say I have this function using the nodes classification :

function example(){

var exampleVar = nodes.classfication;

//below i use exampleVar to do calculations
.
.
.
.
}

but say sometimes i want to get the nodes status so I do this :

function example(){

    var exampleVar = nodes.status;

    //below i use exampleVar to do calculations
    .
    .
    .
    .
    }

I dont want repeating code as (in my code) there is quite a lot of code in the function i want to reuse. So depending what I want to get out of this function ill pass it to the function like so :

function example(thisAttribute){

    var exampleVar = nodes.thisAttribute; //using the variable passed to the function

    //below i use exampleVar to do calculations
    .
    .
    .
    .
    }

This doesn't work, how do I go about passing an object attribute as a parameter to a function ? I've tried looking online but I dont think Im searching for the correct things as I can't find anything to help. Thanks in advance anyway :)

thatOneGuy
  • 9,977
  • 7
  • 48
  • 90
  • Related, potential duplicate: [JavaScript property access: dot notation vs. brackets?](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – James Thorpe May 14 '15 at 14:34

1 Answers1

5

Use object bracket notation:

function example(thisAttribute){
  var exampleVar = nodes[thisAttribute];
}
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79