Created
February 24, 2012 04:11
-
-
Save mgroves/1897521 to your computer and use it in GitHub Desktop.
PrototypeJS AOP
This file contains hidden or 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
| <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