Was the variable x eternal?

In Javascript, is there a good way to check if a variable has ever been true (or any value) in an entire session? The best way that I can think of now is to regularly perform such a check by writing the veracity in another variable:

if (variable){ variablewasevertrue = true; } 

Then, when I want to know if the original variable was ever true, I check if the new variablewasevertrue true or undefined . There is nothing graceful like if (variable was ever true){ ? This is not like Javascript-y.

+4
source share
6 answers

There is no if (variable was ever true) language in the language. Variables store values, not history.

Intercepting values ​​and validating is the only way to do this. If the variable is indeed a property (for example, a global variable is a property of a global object), you can easily intercept changes using setters .

So, to have a global history variable that you could do

 var hasEverBeenTruthy = false; (function () { var value; Object.defineProperty(window, "myGlobal", { "get": function () { return value; }, "set": function (newval) { if (newval) { hasEverBeenTruthy = true; } value = newval; } }); })(); 

This will work with modern browsers, and many older browsers have __defineSetter__ options.

+8
source

Variables store a value, not a memory location history. If you want to do something like this, I would suggest you use an object of some kind:

 var x = { value: false, history: [], set: function(val){ history.push(this.value); this.value = val; }, wasEver: function(val){ return this.history.indexOf(val) >= 0; } }; 

Then you can use the object as follows:

 x.set(false); x.value; //returns false x.set(true); x.value; //returns true x.wasEver(true); // returns true x.wasEver(false); //returns true x.wasEver("hello"); //returns false 

This gives each object in which it has a history (for example, it can check several values, and not just one - as is the case with the recipient / installer material mentioned in other answers), and is guaranteed to work in any field, since all the functionality contained in a particular object.

+7
source

No, except that you can use getter and setter like this, which delegates the variable parameter so that you can check if it will be set at a time:

 var value, wasevertrue = false; window.__defineSetter__('test', function(v) { value = v; wasevertrue = wasevertrue || (v === true); }); window.__defineGetter__('test', function() { return value; }); 

Now,

 test = false; // wasevertrue === false test = true; // wasevertrue === true test = false; // wasevertrue === true 

It would be even better to put this in closure, because now you can simply set value = true as a workaround for the installer.

+5
source

no - there is no monitoring of the state of variables. this is just what its current value is. in addition, this is your own custom implementation using method properties to track state.

+1
source

There is another variable called wasevertrue = false. At any place where you set a "variable", immediately follow it with a check that sees if the variable == true. If so, set wasevertrue = true.

+1
source

You cannot use a single scalar variable to track history, but you can use an array. This is not ideal, but it is an alternative:

 function setVar(arr, value) { arr.unshift(value); return arr; } function getVar(arr) { return arr[0]; } function checkHist(arr, histValue) { var ii; for (ii = 0; ii < arr.length; ii++) { if (arr[ii] === histValue) { return true; } } return false; } var myVar = []; myVar = setVar(myVar, true); myVar = setVar(myVar, false); alert(checkHist(myVar, true)); // alerts "true" 
+1
source

All Articles