0
for(var i=0; i<Elements.length; i++)
{   if( Elements[i].y < 0 )
    {
       console.log({y:Elements[i].y, E:Elements[i]});
    }
}

==========================================================
and the console shows to me next result:

Object {y: -100, E: Element}
    E: Element
      x: 200
      y: 200
    __proto__: Element

   y: -100
   __proto__: Object

==============================

It is pretty strange. The Elements[i].y has -100, whereas within itself Elements[i] the "y" has 200
One object has different data at a time.
Example of the console is for illustrative purposes.
The expected value is 200 instead of -100.
What's wrong? Please help.

abdulmanov.ilmir
  • 1,325
  • 4
  • 15
  • 24
  • 1
    Consoles are funny creatures. When you do `y:Elements[i].y`, you're copying the value of `.y` to the new object's property. But when you do `E:Elements[i]`, you're making a reference to the object. So when you finally view the data in the console, you're seeing a snapshot of `.y` via the `y` property, but a current picture of the entire object via the `E` property. In other words, the copy of `.y` is the value when the loop is running, but the `E:` reference shows the value when the console is displayed. –  Jul 14 '13 at 17:08
  • ...try this: `console.log({y:Elements[i].y, E:JSON.stringify(Elements[i])});`. Now assuming the properties are enumerable, you'll get a serialized snapshot of the object (at least it's non-inherited properties), as it looked during the loop. –  Jul 14 '13 at 17:12
  • 1
    possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Bergi Jul 14 '13 at 17:12
  • @Crazy Train: thanks for your reply. I have tried JSON.stringify, it shows -100, but it does not solve the problem because the program produces different results. The expected value is 200 instead of -100. – abdulmanov.ilmir Jul 14 '13 at 17:20
  • You have some code somewhere that is changing it to `-100`. Must be happening sometime after the loop. Can't tell from the code in your question. –  Jul 14 '13 at 17:23
  • @Crazy Train: What could happen while command "console.log()" is running? – abdulmanov.ilmir Jul 14 '13 at 17:36
  • @ILMIR: Nothing. But something is happening after it's running. In other words, the loop runs and invokes `console.log()`, and you see the correct values when you use `JSON.stringify()`. But then sometime after the loop, but before you need to use the data, the values are being changed. So you need to track down where that's happening. –  Jul 14 '13 at 17:39
  • @Crazy Train: My fault. As you said my object data were changed after the loop. Thanks for help! :) – abdulmanov.ilmir Jul 14 '13 at 18:15

0 Answers0