Last active
August 29, 2015 13:59
-
-
Save AGoblinKing/10935890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TimeTraveler = (function() { | |
var time, | |
def = function(value) { | |
var values = {}, | |
timeTraveler = { | |
get value() { | |
var lastTime = Object.keys(values) | |
.filter(function(pastTime) { | |
return pastTime < def.time; | |
}) | |
.sort(function(a, b) { return a < b ? - 1 : a === b ? 0 : 1;}) | |
.pop(); | |
return values[lastTime]; | |
}, | |
set value(newValue) { | |
values[def.time] = newValue; | |
} | |
}; | |
timeTraveler.value = value; | |
return timeTraveler; | |
}; | |
Object.defineProperty(def, "time", { | |
get : function() { | |
return time === undefined ? (new Date()).getTime() : time; | |
}, | |
set : function(pTime) { | |
time = pTime; | |
} | |
}); | |
return def; | |
} ()); | |
var foo = TimeTraveler(200); | |
setTimeout(function() { | |
foo.value = 500; | |
TimeTraveler.time = (new Date()).getTime() - 200; | |
console.log(foo.value); | |
}, 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment