Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. 'use strict'
  2.  
  3. function Obj() {}
  4.  
  5. Obj.prototype.getVal = getVal
  6. Obj.prototype.clearCache = function clearCachedVal() {
  7. console.log('clearing the cache')
  8. this.getVal = getVal
  9. }
  10.  
  11. function getVal(x) {
  12. var _val
  13. console.log('setting cached data')
  14. this.getVal = function getCachedVal() {
  15. console.log('serving cached data')
  16. return _val
  17. }
  18. //do stuff with _val
  19. _val = x
  20. return _val
  21. }
  22.  
  23. var obj = new Obj()
  24.  
  25. console.log(obj.getVal(1))
  26. console.log(obj.getVal(2))
  27. obj.clearCache()
  28. console.log(obj.getVal(2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement