Created
July 7, 2015 21:28
-
-
Save lykkin/9831f8bb88eccd1a61e1 to your computer and use it in GitHub Desktop.
closure based caching
This file contains hidden or 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
'use strict' | |
function Obj() {} | |
Obj.prototype.getVal = getVal | |
Obj.prototype.clearCache = function clearCachedVal() { | |
console.log('clearing the cache') | |
this.getVal = getVal | |
} | |
function getVal(x) { | |
var _val | |
console.log('setting cached data') | |
this.getVal = function getCachedVal() { | |
console.log('serving cached data') | |
return _val | |
} | |
//do stuff with _val | |
_val = x | |
return _val | |
} | |
var obj = new Obj() | |
console.log(obj.getVal(1)) | |
console.log(obj.getVal(2)) | |
obj.clearCache() | |
console.log(obj.getVal(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment