Skip to content

Instantly share code, notes, and snippets.

@kaleb
Created July 10, 2014 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaleb/fdef61060a766444d7b5 to your computer and use it in GitHub Desktop.
Save kaleb/fdef61060a766444d7b5 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
/**
* Wrap JavaScript properties with before and after methods
*/
function wrapPropertySetter(before, after, obj, propName) {
var propDescr = Object.getOwnPropertyDescriptor(obj, propName);
var set = propDescr.set;
var value = propDescr.value;
if('value' in propDescr || 'writable' in propDescr) {
delete propDescr.value;
delete propDescr.writable;
propDescr.get = function() {
return value;
};
set = function(val) {
value = val;
};
}
propDescr.set = function(val) {
before(val);
set(val);
after(val);
};
Object.defineProperty(obj, propName, propDescr);
}
function wrapPropertySetters(obj, props) {
for(var name in props) {
var prop = props[name];
wrapPropertySetter(prop.before, prop.after, obj, name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment