How can I pass an attribute of objects as a parameter?
For example, I have this function using node classification:
function example(){
var exampleVar = nodes.classfication;
//below i use exampleVar to do calculations
.
.
.
.
}
but sometimes I want to get the status of the nodes, so I do this:
function example(){
var exampleVar = nodes.status;
//below i use exampleVar to do calculations
.
.
.
.
}
I don't want to repeat code like (in my code) there is quite a lot of code in the function I want to reuse. So, depending on what I want to extract from this function, pass it like this:
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 function parameter? I tried looking online but I don't think Im looking for the right things as I can't find anything to help. Thanks in advance anyway :)
+3
source to share
1 answer
Use object binding notation :
function example(thisAttribute){
var exampleVar = nodes[thisAttribute];
}
+5
source to share