2

I have a javascript object called myObject. Inside that object i have a property called x that has a value of 0. After i have incremented x value by 1 i want to reset it back to 0, without accessing x to change it, but it does not work for some reason. I am using the Module Pattern.

var MyObject = (function(){

return {
     x:0
}
})();

var myObject = MyObject;

myObject.x+=1;
console.log(myObject.x); // shows value of 1

myObject = MyObject;  // object property reset
console.log(myObject.x); // shows value of 1, but i want 0
Pleinair
  • 437
  • 9
  • 21

2 Answers2

3

You are not resetting your object. You need to call the function each time.

var MyObject = (function() {
  return {
    x: 0
  }
});

var myObject = MyObject();
myObject.x += 1;
console.log(myObject.x); // shows value of 1

myObject = MyObject(); // object property reset
console.log(myObject.x); // shows value of 0
  • 1
    This answer is *wrong*. OP is using an IIFE to assign a value to `MyObject` and therefore the value of `MyObject` is not a function and consequently trying to do `myObject = MyObject();` will result in `TypeError: MyObject is not a function` – rabbitco Nov 20 '16 at 21:51
  • @rabbitco In this example, although the function is inside parentheses (expression), it is not being invoked, so MyObject gets assigned the value that expression evaluates to, which is indeed a function. – JJWesterkamp Nov 20 '16 at 22:05
  • 1
    @JeffreyWesterkamp: That is incorrect. OP code is `var MyObject = (function() {return {x: 0}})();`. So it *is* invoked. Copy paste OP's code and run it and you will see for yourself. – rabbitco Nov 20 '16 at 22:23
  • Yes it is, but in this answer it is not. That's what I meant, this answer actually should work. – JJWesterkamp Nov 20 '16 at 22:25
  • @JeffreyWesterkamp: ok you are right about that. But the answer leaves the impression that OP - in his own code - just needs to change `myObject = MyObject;` to `myObject = MyObject();` and that won't work. – rabbitco Nov 20 '16 at 22:29
  • Besides why is the function encapsulated in `()` - that makes no sense. I think SoftwareEngineer just did a poor job of copy-pasting OP's code ... – rabbitco Nov 20 '16 at 22:31
  • @rabbitco I agree it is a little confusing, and those parens better not be there either. – JJWesterkamp Nov 20 '16 at 22:32
2

Javascript object reset not working

That is because you are re-assigning a reference to the same object.

What your are overlooking in your code

You are using an IIFE (Immediately-Invoked Function Expression) to initially assign the value {x: 0} to MyObject. You are not assigning a function to MyObject:

var MyObject = (function() {
  return {
    x: 0
  }
})();

console.log(MyObject); // { x: 0 }

That is because the IIFE only executes once and then ceases to exist. When you then do var myObject = MyObject; you are not creating a copy of MyObject but just another reference to the object { x: 0 }:

var a = {x: 0};
var b = a;

console.log(b); // {x: 0}

b.x = 10;

console.log (a); // 10
console.log (b); // 10

That is why your myObject = MyObject; does not reset anything because you are actually just restating that myObject should point to the same object as MyObject.

How you can make it work

Change your code to:

var MyObject = function(){
    return {x: 0}
}

var myObject = MyObject();

myObject.x+=1;
console.log(myObject.x); // 1

myObject = MyObject();  
console.log(myObject.x); // 0
Community
  • 1
  • 1
rabbitco
  • 2,790
  • 3
  • 16
  • 38