0

I am using a setter to give a variable in my class a new value. However, after the value is set and is used in another method the variable defaults back to its original value.

The following code is not the actual code in my project....

var canContinue = false;

classXX.prototype.check = function() {

     if(canContinue){
          //do something
     }
}

classXX.prototype.init = function() {
   canContinue = false;
}

classXX.prototype.setCanContinue(val) {
   canContinue = val;  
}

return {
        getInstance: function() {
            _instance = new classXX();
            return _instance;
        }
    };

After the class has been instantiated canContinue = true; If i make the call classXX.setCanContinue(false) then the check function still sees canContinue as true.

Am I missing something?

urnotsam
  • 770
  • 7
  • 24
  • please provide a self contained runnable example. Where is classXX? – hugomg May 27 '15 at 16:37
  • 4
    Why is canContinue not a class variable? `classXX.prototype.canContinue` would allow it to be assigned and referenced by `this.canContinue`. – Jesse Kernaghan May 27 '15 at 16:37
  • 1
    setCanContinue is wrong defined and why did you not use an instance(object) property instead of a kind of class property where all objects use the same variable? – Blauharley May 27 '15 at 16:38
  • @Jesse: putting a mutable variable in classXX.prototype is a recipe for headaches. Either its a global variable or its an instance variable that should be initialized in the constructor. – hugomg May 27 '15 at 16:38
  • This is a very small and not a 100% accurate representation of the current class. – urnotsam May 27 '15 at 16:41

1 Answers1

2

You can do something like below, but the canContinue variable will be shared between all the instances. if you want that to be specific to each instance then you should declare it inside constructor function.

var classXXfactory = (function() {

  var canContinue = false;

  function classXX() {

  }

  classXX.prototype.check = function() {
    if (canContinue) {
      console.log('continue');
    }
  }

  classXX.prototype.init = function() {
    canContinue = false;
  }

  classXX.prototype.setCanContinue = function(val) {
    canContinue = val;
  }

  classXX.prototype.getCanContinueValue = function() {
    return canContinue;
  }

  return {
    getInstance: function() {
      _instance = new classXX();
      return _instance;
    }
  };

}());


console.log('############### First Instance');

var inst1 = classXXfactory.getInstance();

console.log(inst1.getCanContinueValue());
inst1.setCanContinue(true);
console.log(inst1.getCanContinueValue());

console.log('############### Second Instance');

var inst2 = classXXfactory.getInstance();

console.log(inst2.getCanContinueValue());
inst2.setCanContinue(true);
console.log(inst2.getCanContinueValue());

console.log('############### Setting value in second Instance');

inst2.setCanContinue("SETTGIN IN SECOND INST2");

console.log('############### Getting value from both Instances');
console.log(inst1.getCanContinueValue());
console.log(inst2.getCanContinueValue());
jad-panda
  • 2,509
  • 16
  • 22
  • this doesnt work for me, but it should. Not sure whats going on with my code. I will upvote this for good OOP design – urnotsam May 27 '15 at 16:55