Javascript Variable History
I recently answered a Stackoverflow Question where a user wanted to know if there was a way, in JavaScript, to check if a variable has ever been a particular value. Of course, there is no default built-in way to do this, but I provided (what I think to be) a valid answer. However, after a few minutes, I looked at it and thought that it might be able to help other people as well. So here it is - Chronicle. Chronicle lets you define a special type of object, and you can check the history of that object at any point in time. Here's a quick example:
var isValid = new Chronicle(false); // Default the value to false
isValid.set("not valid"); // Set it to a string
isValid.set(true); // Set it to true
// This will return true if the value inside this Chronicle
// has ever been true, and return false otherwise.
isValid.was(false);
If anyone wants this functionality on your site, it's as easy as including/running the following javascript anywhere before you need to use a Chronicle (as long as it's within the proper scope, of course). Chronicle works in any scope, in any situation.
function Chronicle(val){
this.value = val;
this.history = [];
if(val != undefined){
this.history.push(val);
}
}
Chronicle.prototype.set = function(val){
this.value = val;
this.history.push(val);
};
Chronicle.prototype.was = function(val){
return this.history.indexOf(val) >= 0;
};
Chronicle.prototype.toString = function(){
return this.value.toString();
}