javascript - what mean of calling command (param1, param2, ...) -
i read e-book , find command, don't know it's purpose , when using it. example:
var x = (12, 34, 56)
x in case number , it's value 56. thanks.
this full script read ebook 'javascript absolute beginners':
var fridge = { buttermilk: [1/3, "cup"], kefir: [1 + 1/2, "cup"], yogurt: [4, "cup"], }; var dough = { hardwhitewholewheatflour: [2, "cup"], sugar: [1/3, "cup"], madagascarvanilla: [1, "tsp"], orangezest: [1, "tbs"], soda: [1, "tsp"], tartar: [1, "tsp"], orangejuice: [1/2, "cup"], culturedmilk: [1/2, "cup"], egg: [1], cranberries: [2/3, "cup"] }; dough.culturedmilk[0] = fridge.buttermilk[0] >= 1/2 ? (fridge.buttermilk[0] -= 1/2, 1/2) : fridge.kefir[0] >= 9/16 ? (fridge.kefir[0] -= 9/16, 9/16) : fridge.yogurt[0] >= 10/16 ? (fridge.yogurt[0] -= 10/16, 10/16) : alert("no cranberry bread you!"); dough.culturedmilk; // [0.5625, "cup"] fridge.kefir; // [0.9375, "cup"]
it seems know means... evaluated each operand , return result of last.
in case of (fridge.buttermilk[0] -= 1/2, 1/2)
, expression part of conditional operator:
fridge.buttermilk[0] >= 1/2 ? (fridge.buttermilk[0] -= 1/2, 1/2) : ...
in case, comma operator used introduce side effects. if fridge.buttermilk[0] >= 1/2
true, 1/2
should assigned dough.culturedmilk[0]
. @ same time, fridge.buttermilk[0]
should updated well.
normally use 2 expression statements, like
if(fridge.buttermilk[0] >= 1/2) { fridge.buttermilk[0] -= 1/2; dough.culturedmilk[0] = 1/2; }
but since author wants use conditional operator, comma operator can used execute both expression in 1 expression. updates value of fridge.buttermilk[0]
, returns 1/2
.
i'm not recommending doing though, in case, nested conditional operators, traditional if-else
statement easier read.
Comments
Post a Comment