summaryrefslogtreecommitdiffstats
path: root/samples/middleware.js
diff options
context:
space:
mode:
Diffstat (limited to 'samples/middleware.js')
-rw-r--r--samples/middleware.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/samples/middleware.js b/samples/middleware.js
new file mode 100644
index 0000000..e572d10
--- /dev/null
+++ b/samples/middleware.js
@@ -0,0 +1,24 @@
+function foo() {
+ this.tag = 'bar'
+ this.bar = function(txt) {
+ console.log('hello ' + txt);
+ }
+}
+
+var bar = new foo();
+
+function middleware(foo) {
+ oldbar = foo.bar; /* save old function, don't make a reference! */
+ foo.bar = function(txt) {
+ /* ah, here's our chance to mutate txt argument! */
+ txt = 'dozed off ' + txt;
+ oldbar(txt);
+ }
+
+ foo.newtag = 'muhahaha';
+}
+
+middleware(bar); /* mutate! */
+bar.bar('kyle');
+console.log(bar.newtag);
+