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?
- 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
4 Answers
Reset to default 10No, 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
版权声明:本文标题:javascript - Get all values of variable using JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240867a2649320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论