0

So given the following code my expected behaviour would be

console.log(charts[i-1]['yaxes'][0]['min']);        
options[chart_number]['yaxes'][0]['min'] = charts[i]['yaxes'][0]['min'];
console.log(charts[i-1]['yaxes'][0]['min']);

output some value, in this case: 1356.6
set options[chart_number]['yaxes'][0]['min'] equal to another value, in this case 0
output the same value as expected on line 1: 1356.6

Instead my output for the third line is 0, leading me to believe that the line before is responsible for changing this arrays value.

Anyone have any ideas why setting a subvalue of options equal to a value from a different array is changing the source array as well?

Update:

Changing it to

console.log(charts[i-1]['yaxes'][0]['min']);        
options[chart_number]['yaxes'][0]['min'] = charts[i]['yaxes'].slice(0)[0]['min'];
console.log(charts[i-1]['yaxes'][0]['min']);

Doesn't appear to have helped at all. My code base is probably too long to paste here and would probably involve you digging for a while to help me but thanks for the suggestion if a potential avenue of exploration.

Update 2:
Demo of the full problem here: http://jsfiddle.net/SxW3X/

Add a dataset, like "unemployment", Hit RSI, move RSI to second chart, Chart 1 maintains the new min set, through the code above.

James Roe
  • 1
  • 2

1 Answers1

0

In javascript, all non-primitive values are by default passed by reference, so you should look where you build options and charts array, use the Array slice method if possible if you want to hard copy non-primitive types like arrays.

Rich
  • 5,603
  • 9
  • 39
  • 61
Kemal Dağ
  • 2,743
  • 21
  • 27
  • Could I be running into this issue if i am pushing objects onto charts[]? That's the only time I am touching it. I was under the impression jquery push created a new object, does it just pass a reference? – James Roe Jun 24 '13 at 08:49
  • Javascript's default behaviour is pass by reference, so yes, it is the case in your situation. See the best answer in this question http://stackoverflow.com/questions/122102/most-efficient-way-to-clone-an-object, try a shallow copy. And notice that, pass by reference it to increase performance, you may design your app keeping this principle in mind. – Kemal Dağ Jun 24 '13 at 08:52
  • 1
    Careful, the term "pass by reference" is a well defined term for a specific way of parameter passing. JavaScript is **not** pass by reference, it is pass by value. In case of objects, that value *is* a reference, but that's not pass by reference. See http://en.wikipedia.org/wiki/Evaluation_strategy and http://stackoverflow.com/q/518000/218196. – Felix Kling Jun 24 '13 at 08:58
  • Slice doesn't appear to have fixed anything, it's an array not an object so the jquery extend method doesn't appear to be applicable. Thanks for the potential avenue of exploration. – James Roe Jun 24 '13 at 09:18
  • @felixKling thanks for the clarification. My use of "pass by reference" may lead to misunderstandings. Yet, what I tried to explain was, when playing around pointers to JavaScript objects and arrays, one should be carefull and know exact behaviour of Javascript. – Kemal Dağ Jun 24 '13 at 10:17