Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Last active December 19, 2015 16:48
Show Gist options
  • Select an option

  • Save thomasboyt/5986556 to your computer and use it in GitHub Desktop.

Select an option

Save thomasboyt/5986556 to your computer and use it in GitHub Desktop.
proxies vs observers
<!-- Works in Chrome with "Experimental JavaScript Features" flag enabled -->
<html>
<body>
<span id="test"></span>
<script>
var foo = {};
Object.observe(foo, function(records) {
records.forEach(function(record) {
document.getElementById(record.name).innerHTML = record.object[record.name];
});
});
foo['test'] = "Hello.";
</script>
</body>
</html>
<!-- Works in Firefox -->
<html>
<body>
<span id="test"></span>
<script>
var foo = new Proxy({}, {
set: function(target, name, val) {
document.getElementById(name).innerHTML = val;
target[name] = val;
}
});
foo['test'] = "Hello.";
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment