Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <script>
- var deepClone = function (smth) {
- if (typeof smth === 'object') {
- if (smth.constructor === Array) {
- cloned = []
- for (i = 0; i < smth.length; i++) {
- cloned[i] = deepClone(smth[i]);
- }
- return cloned;
- }
- cloned = {};
- for (p in smth) {
- cloned[p] = deepClone(smth[p]);
- }
- return cloned;
- }
- else {
- return smth;
- }
- }
- var makeObservable = function (o) {
- for (p in o) {
- // store value in clojure because accessing it through the object itself in get/set
- // would trigger get/self again ad infinitum
- currentValue = o[p]
- Object.defineProperty(o, p, {
- get: function() {
- console.log("Getting property " + p + " value is " + currentValue);
- return currentValue;
- },
- set: function(newValue) {
- console.log("Setting property " + p + " of value " + currentValue + " to the value " + newValue);
- currentValue = newValue;
- }
- })
- }
- };
- var o = {a: 1};
- makeObservable(o);
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment