Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS console showing wrong results

I get a strange behaviour with Firefox when modifying a variable and displaying it (live demo here):

var MyModule = ( function() {
  var currentPosition = {x : 1, y : 2, z : 3};

  function changePosition() { currentPosition.x = 17; };

  return { changePosition : changePosition,    
           currentPosition : currentPosition };  
} )();

console.log(MyModule.currentPosition);  // 17, 2, 3 instead of 1, 2, 3 !!
MyModule.changePosition();
console.log(MyModule.currentPosition);  // 17, 2, 3

Why did this happen ? (Why does current.Position give 17 before it was modified to 17?)

More generally, how to get/set a variable in a Revealing Module Pattern?


Screenshot with Firefox:

enter image description here

like image 507
Basj Avatar asked Dec 01 '25 13:12

Basj


1 Answers

I am going to guess that you ran the 3 lines one after each other and when you inspect the first printed line there is a little i next to it. Chrome will report the the most recent attributes of an object not the value of them at time of printing so if you were to just print the currentPosition and not run changePosition() then you would see 1,2,3.

like image 156
Quince Avatar answered Dec 04 '25 04:12

Quince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!