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

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

7 Comments

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 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.
@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.
Yes it is, but in this answer it is not. That's what I meant, this answer actually should work.
@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.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.