Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior with a Javascript array, object and RaphaelJS
    primarykey
    data
    text
    <p>I have a situation of defining an object with an array in it. For some reason Chrome adds artifacts to that object before I try to use it.</p> <p>My Object: <br/></p> <pre><code>var colorPalettes = { single_A7DBD8: { amounts: [100], colors: ['#A7DBD8'] }, single_C02942: { amounts: [100], colors: ['#C02942'] } } </code></pre> <p>When I run the code, the console shows the following content for <code>var colorPalettes</code>:<br/></p> <pre><code>amounts: Array[1] 0: Object //&lt;-- added by Chrome, 'value' should come directly in the array order: 0 value: 100 valueOf: function (){return this.value} __proto__: Object length: 1 __proto__: Array[0] colors: Array[1] 0: "#A7DBD8" length: 1 __proto__: Array[0] __proto__: Object </code></pre> <p><strong>The questions are: <br/></strong></p> <ol> <li>Why does <code>amounts</code> have an <code>Object</code> inside the array? - I didn't define it.</li> <li>If so - why isn't <code>colors</code> demonstrating the same behavior?</li> <li>Why is this behavior not demonstrated in my code when I run it through jsfiddle?</li> </ol> <p><strong>What did I try: <br/></strong></p> <ol> <li>In my code <code>colorPalettes</code> is set as <code>window.something.colorPalettes</code>. I turned it into a local variable - no success. </li> <li>I tried swapping the order of <code>amounts</code> and <code>colors</code> (I know, I was desperate...) - so the behavior also swapped, now <code>colors</code> had the extra <code>Object</code>.</li> <li>I put my code into <a href="http://jsfiddle.net/2JfyX/4/" rel="nofollow">http://jsfiddle.net/2JfyX/4/</a> but the behavior was normal - no extra <code>Object</code> inside <code>colorPalettes</code>.</li> </ol> <p><strong>Edit 1: <br/></strong></p> <p>I also tried to put a string inside <code>amounts</code> instead of its current <code>int</code>.</p> <p><strong>Edit 2: <br/></strong> Tried to simulate adding a jQuery click event as in my code, was not able to reproduce <a href="http://jsfiddle.net/2JfyX/19/" rel="nofollow">http://jsfiddle.net/2JfyX/19/</a></p> <p><strong>Edit 3: <br/></strong> I believe I have found the culprit. It is g.Raphael. For some reason (didn't delve into their code yet), it makes changes to the object I pass to it <br/></p> <p>You can see that I directly pass <code>colorPalettes</code> to r.piechart and in the 6th loop it changes my array to contain an object. Not nice :-O</p> <p>I will clone the arrays from now on and hope for the best...</p> <pre><code>function renderPalettes(colorPalettes, radius) { var gap = radius + 20; for (var palette in colorPalettes) { var paletteObj = r.piechart(gap, 43, radius, colorPalettes[palette].amounts, { colors: colorPalettes[palette].colors, stroke: "#cdcdcd", strokewidth: '0.3' }); for (var i = 0; i &lt; paletteObj.series.items.length; i++) { paletteObj.series.items[i].node.setAttribute('class', palette + ' sliderPalette'); } gap = gap + radius * 2 + 24; } } </code></pre> <p>Any idea? Thanks, D.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload