Created
November 27, 2015 23:27
-
-
Save anonymous/2c952c2545b1ee88f098 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/misediquna
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id="content" data-each-item="links"> | |
</div> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
function observable(object, prop, watcher) { | |
var config = { enumerable: true }; | |
Object.defineProperty(object, "__" + prop + "__", { | |
enumerable: false, | |
value: object[prop] | |
}); | |
config.get = function () { | |
return object["__" + prop + "__"]; | |
}; | |
if (watcher) { | |
config.set = function (newVal) { | |
watcher(newVal, object[prop]); | |
object["__" + prop + "__"] = newVal; | |
}; | |
} | |
Object.defineProperty(object, prop, config); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function observable(object, prop, watcher) { | |
var config = {enumerable: true}; | |
Object.defineProperty(object, `__${prop}__`, { | |
enumerable: false, | |
value: object[prop] | |
}); | |
config.get = () => { | |
return object[`__${prop}__`]; | |
} | |
if (watcher) { | |
config.set = (newVal) => { | |
watcher(newVal, object[prop]); | |
object[`__${prop}__`] = newVal; | |
}; | |
} | |
Object.defineProperty(object, prop, config); | |
} | |
</script></body> | |
</html> |
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 observable(object, prop, watcher) { | |
var config = { enumerable: true }; | |
Object.defineProperty(object, "__" + prop + "__", { | |
enumerable: false, | |
value: object[prop] | |
}); | |
config.get = function () { | |
return object["__" + prop + "__"]; | |
}; | |
if (watcher) { | |
config.set = function (newVal) { | |
watcher(newVal, object[prop]); | |
object["__" + prop + "__"] = newVal; | |
}; | |
} | |
Object.defineProperty(object, prop, config); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment