javascript - Is it possible to reference a variable -
i have object 30 'values' or whatever called, example:
var list = new object(); list.one = 0; list.two = 0; list.three = 0; ... list.thirty = 0;
is there simple/efficient way me able increment values of list object?
for example let's there 100 buttons, each button if pressed increase value amount. i'm trying short function like:
function test( value, amount, math ) { if( math === "add" ) { value += amount; } else if( math === "sub" ) { value -= amount; } }
but whole value thing doesnt work =(. other way can think doing creating 30 functions, each function same thing above each 1 specifies list value add or subtract to. or make single function if value === "one" list.one etc. other ideas? i've thought using array i'd prefer having code easy read since list values have specific names make easy me in other functions.
thanks in advance help
is possible reference variable?
no. possible reference object, , use variable property names. that's have, change function to
function test( value, amount, math ) { if( math === "add" ) { list[value] += amount; } else if( math === "sub" ) { list[value] -= amount; } }
and call test("one", 100, "add")
instead of test(list.one, 100, "add")
. btw, i'd recommend use negative values instead of add/sub
verbs.
Comments
Post a Comment