Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Last active December 19, 2015 19:59
Show Gist options
  • Save thomasboyt/6010501 to your computer and use it in GitHub Desktop.
Save thomasboyt/6010501 to your computer and use it in GitHub Desktop.
js getters and setters
var o = {};
Object.observe(o, function(records) {
console.log(records);
});
// the following three changes are batched, as they occur
// within the same frame of the loop
o.x = "foo";
o.y = "bar";
o.z = "baz";
// these two instances will be batched separately
// in their own frame
setTimeout(function() {
o.a = "bah";
o.b = "blergh";
}, 100);
var o = {
"_x": "foo",
get x() {
return this._x;
},
set x(val) {
this._x = val;
}
};
console.log(o.x); // "foo"
o.x = "bar";
console.log(o.x); // "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment