Value holder

I have something in my mind that I don’t know if its possible to achieve using javascript.

Let’s explain the scenario:
-We have input that has a number value: 1 to 100.

var myvalue = input.value; //(Lets say input has 50 as value).
var sum = {
  a: myvalue,
  get b() { return this.a; }
}; 
console.log(myvalue.a);
console.log(myvalue.b);

I’m not sure if i can explain what im trying to do :smashy:

Actually when the page opens, or value changes, the console log prints on both (a,b) the same value.
My objective its:

  • The option a holds the new value (after the update, i.e starting at 50, and when input value changes get the new value).
  • The option b holds the last value (In this case will be 50, and only updates if I do manually).

Why? Because I want to do something like this:

  if (sum.a != sum.b) {
    //do something
    sum.b = sum.a //Update the option B to the new value for next call.
  }

Thanks!!

var a = input.value
var b = input.value
console.log(a,b)
document.getElementById('valuefield').addEventListener('change', function() {
//New Value cannot be old value, or change would not have fired.
//Do Something
b = a;
a = input.value;
console.log(a,b);
});
1 Like

Thank you @m_hutley that should work very good, but my point is what if there’s no input and it’s a global variable that holds a number?

I should ask that better because I’m more interested in how to do that scenario using a global variable number than a field because I’m finding most of the cases need to figure out how to play with the data changes.

Thanks!!

a and b are both global variables.

I think what you’re trying to describe is the concept of getters and setters.

var myobj = {
   curval: 50,
   oldval: 50,
   moveToB() {
     this.oldval = this.curval;
   },
   get a() {
      return this.curval
   },
   set a(value) {
      this.moveToB();
      this.curval = value;
      console.log(this.curval,this.oldval);
   }
}
myobj.a = 5;
<< 5 50

Thanks i’m configuring it with my vars. Btw what means that last lines?

Was just an indication of what output you’d get.

Thanks, i configured it as you did but using my vars:

var upvalue = properties.value;
  
var bcount = {
   nuevo: upvalue, //current
   viejo: upvalue, //old
   moveToB() {
     this.viejo = this.nuevo;
   },
   get a() {
      return this.nuevo
   },
   set a(value) {
      this.moveToB();
      this.nuevo = value;
      console.log(this.nuevo,this.viejo);
   }
}

But when the page loads / or the value changes, it doesn’t post anything in console. Thanks!

Does bcount.a change somewhere in your code?

No,
This is my code:

var upvalue = properties.value;
var bcount = {
   nuevo: upvalue, //current
   viejo: upvalue, //old
   moveToB() {
     this.viejo = this.nuevo;
   },
   get a() {
      return this.nuevo
   },
   set a(value) {
      this.moveToB();
      this.nuevo = value;
      var radialObj = $("#"+properties.id).data('radialIndicator');
	  radialObj.animate(value);
      console.log(this.nuevo,this.viejo);
   }
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.