Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <script>
  9.  
  10. var deepClone = function (smth) {
  11. if (typeof smth === 'object') {
  12. if (smth.constructor === Array) {
  13. cloned = []
  14. for (i = 0; i < smth.length; i++) {
  15. cloned[i] = deepClone(smth[i]);
  16. }
  17. return cloned;
  18. }
  19. cloned = {};
  20. for (p in smth) {
  21. cloned[p] = deepClone(smth[p]);
  22. }
  23. return cloned;
  24. }
  25. else {
  26. return smth;
  27. }
  28. }
  29.  
  30. var makeObservable = function (o) {
  31. for (p in o) {
  32. // store value in clojure because accessing it through the object itself in get/set
  33. // would trigger get/self again ad infinitum
  34. currentValue = o[p]
  35. Object.defineProperty(o, p, {
  36. get: function() {
  37. console.log("Getting property " + p + " value is " + currentValue);
  38. return currentValue;
  39. },
  40. set: function(newValue) {
  41. console.log("Setting property " + p + " of value " + currentValue + " to the value " + newValue);
  42. currentValue = newValue;
  43. }
  44. })
  45. }
  46. };
  47.  
  48. var o = {a: 1};
  49. makeObservable(o);
  50. </script>
  51. </body>
  52. </html>
Add Comment
Please, Sign In to add comment