Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created February 24, 2012 04:11
Show Gist options
  • Select an option

  • Save mgroves/1897521 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/1897521 to your computer and use it in GitHub Desktop.
PrototypeJS AOP
<html>
<head>
<title>Javascript AOP Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
String.prototype.capitalize = String.prototype.capitalize.wrap(
function(callOriginal, eachWord) {
if (eachWord && this.include(" ")) {
// capitalize each word in the string
return this.split(" ").invoke("capitalize").join(" ");
} else {
// proceed using the original function
return callOriginal();
}
});
document.observe("dom:loaded", function() {
alert("hello world".capitalize());
alert("hello world".capitalize(true));
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment