Created
July 10, 2014 20:14
-
-
Save kaleb/fdef61060a766444d7b5 to your computer and use it in GitHub Desktop.
This file contains 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> | |
</body> | |
</html> |
This file contains 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
/** | |
* 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