javascript - Cordova / Coffeescript - Weird array when run under iOS -
i'm using cordova in combination coffeescript , try shuffle array. while debugging why function not work, noticed array created not behave should. consider following piece of code:
shufflearray: (arr) -> $log.log("arr:") $log.log(arr) arr2 = new array(arr.length) $log.log("arr2 @ beginning: " + json.stringify(arr2)) = 0 while < arr.length $log.log("i: " + string(i)) $log.log("array before assigning: ") $log.log(arr2) valuetoassign = arr[i] $log.log("value assign:") $log.log(valuetoassign) arr2[i] = valuetoassign $log.log("array after assigning: ") $log.log(arr2) += 1 $log.log("arr2 @ end: ") $log.log(arr2) # ... when executed arr=[1,2,3] following result:
[log] arr: [log] [1, 2, 3] [log] arr2 @ beginning: [null,null,null] [log] i: 0 [log] array before assigning: [log] [3, 2, 1] [log] value assign: [log] 1 [log] array after assigning: [log] [3, 2, 1] [log] i: 1 [log] array before assigning: [log] [3, 2, 1] [log] value assign: [log] 2 [log] array after assigning: [log] [3, 2, 1] [log] i: 2 [log] array before assigning: [log] [3, 2, 1] [log] value assign: [log] 3 [log] array after assigning: [log] [3, 2, 1] [log] arr2 @ end: [log] [3, 2, 1] while expecting "array before assigning" [null, null, null] first time there values yet. let me state out order random , changes execution execution.
so there 2 things cannot figure out:
why arr2 change between when entering while loop?
why array value assigning not work?
and additionally: why not happen when embedded android?
thank you!
this due exact point in time when values read in log. assume debugging webview safari find array in log. if open console values read at time. see final value instead of 1 @ time printed console.
solution: use json.stringify(arr2) @ times see actual value @ point in time.
it have worked if used $log.log("array after assigning: " + arr2), because implicitely call arr2.tostring(), making value immutable after printing console.
also see these questions:
Comments
Post a Comment