admin管理员组

文章数量:1415644

So in my js code I have some global variable that changes its value several times, for example

var x = 0;
...
x = 10;
...
x = 5;

Is there any possibility to get "history" of x without saving its value in other variables? Like, is there some function to detect that at some point of time x was equal to 10?

So in my js code I have some global variable that changes its value several times, for example

var x = 0;
...
x = 10;
...
x = 5;

Is there any possibility to get "history" of x without saving its value in other variables? Like, is there some function to detect that at some point of time x was equal to 10?

Share Improve this question asked Jan 30, 2017 at 9:02 CoffeeCoffee 2,2439 gold badges32 silver badges57 bronze badges 6
  • you got something related to it by google ?? – Brave Soul Commented Jan 30, 2017 at 9:02
  • Without using a history array variable I don't see this working mate - I could be wrong of course. – Terrance00 Commented Jan 30, 2017 at 9:03
  • 1 You can not to achieve this with raw variable, but can with object property, using setters. – vp_arth Commented Jan 30, 2017 at 9:06
  • 2 If you seriously need this all the time in a large application for important purposes, Redux is a library/design pattern/paradigm that makes it possible to track the entire application's state in a kind of "time machine". (It's very plex to learn though, and requires that your entire app is structured using its principles. There may be simpler ways to achieve what you want to achieve.) – Pekka Commented Jan 30, 2017 at 9:06
  • 1 Is it explicit modification in code or modification at run time ? – Amit Kumar Commented Jan 30, 2017 at 9:06
 |  Show 1 more ment

4 Answers 4

Reset to default 10

No, once a value is assigned to a variable, that variable's previous value is overwritten. It isn't retained anywhere. (If it were, it would be a nightmare for memory management.)

You could make an object property that retained a history if you wanted, by using a setter function; rough example:

var obj = {
  _fooValue: undefined,
  fooHistory: [],
  set foo(value) {
    this.fooHistory.push(this._fooValue);
    this._fooValue = value;
  },
  get foo() {
    return this._fooValue;
  }
};

obj.foo = 0;
obj.foo = 5;
obj.foo = 42;
console.log(obj.fooHistory);

In that example, the history doesn't contain the current value, just the previous ones, and it stores the current value in another object property which means code could bypass the setter. There are lots of tweaks you could do. If you thought it was important, you could lock it down more:

var obj = (function() {
  // These two vars are entirely private to the object
  var fooHistory = [];
  var fooValue;
  
  // The object we'll assign to `obj`
  return {
    set foo(value) {
      fooHistory.push(fooValue);
      fooValue = value;
    },
    get foo() {
      return fooValue;
    },
    get fooHistory() {
      // Being really defensive and returning
      // a copy
      return fooHistory.slice(0);
    }
  }
})();

obj.foo = 0;
obj.foo = 5;
obj.foo = 42;
console.log(obj.fooHistory);

You can use variable like array and unshift next value to this array. And to use it take first element:

var x = [];
...
x.unshift(10);
...
x.unshift(5);

var currentX = x[0];
var allValues = x;

Yes there is. Using the Time Travelling debugging in Microsoft Edge browser. Check this out.

I'm not JS specialist, but as mon idea for any OOP language, I would suggest to create special class for x (inherited from Integer in your example), which has overriden setter and some history array list. So, when you set a new value it stored in your history.

You need change only the variable type, not the code, which works with that. I also don't think there is some standard solution for this in any language. Probably, some dynamic introspectors, but those would be even more plex than my idea.

本文标签: javascriptGet all values of variable using JSStack Overflow