So, I have seen examples on AOP in Javascript and most of them either are something like this:
var foo = somefunc.after(function() { // after is some function already made
console.log("after!");
});
foo(); // logs "after"
Like this:
var foo = function() {
console.log("something");
};
foo = (function() {
var original = foo;
return function() {
console.log("before!");
original.apply(this, arguments);
}
})();
foo(); // logs "before" then "after"
Or like this:
function Foo() {
console.log("foo");
}
var original = Foo;
Foo = function() {
original();
moreStuff();
}
function moreStuff() {
console.log("hi");
}
Foo();
// logs "foo" then "hi"
But, what want is a way that does not modify the original function and you do not have to set it to a variable. What I am looking where the you can do this:
function foo() {
console.log("foo!");
}
foo.after(function() {
console.log("after");
});
// and then
console.log(foo);
/* and it will log:
*
* function foo() {
* console.log("foo!");
* }
*
* Original function stays the same
*/
foo();
// logs "something" then "before"
Basically, when the person calls the function it uses the modified version, but when a person just receives the value of the function, foo;
, it returns the original.
How would one achieve this?