0

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?

anonymous
  • 23
  • 8
  • 1
    That's not possible. Functions are immutable. `foo` can't stay the same but do something else. Why would you want to do this? – Bergi May 10 '16 at 22:53
  • @Bergi I want a function to be called before another function is, whenever the function is called. Except, I do not want to modify the contents that are returned when a person does: `func;`, but when the function is called it uses the modified content. – anonymous May 10 '16 at 22:56
  • 1
    As I said, that's not possible. And you cannot "modify the contents of a function" anyway, all you can do is create a new function. Btw, who cares about "function contents" (i.e. their `.toString()` result)? – Bergi May 10 '16 at 22:58
  • @Bergi Yeah, you're probably right (not many people care about function contents). If there was a way to apply a getter to a function that would be neat. Thanks for the help anyways. – anonymous May 10 '16 at 23:02
  • You may been looking to create a class and extend another class with the first. This is possible in Javascript since ES6 : https://medium.com/ecmascript-2015/es6-classes-and-inheritance-607804080906#.u4974s60d for more features look at this : http://es6-features.org/ – Jose Hermosilla Rodrigo May 10 '16 at 23:14
  • 1
    @anonymous: What do you mean by "apply a getter to a function"? It sounds like you're asking for ES6 proxies… and indeed, they could make it look like what you want, but still please [edit] your question to include [what you need this for](http://meta.stackexchange.com/q/66377) (a case where `foo = foo.after(…)` does not suffice). – Bergi May 11 '16 at 01:11
  • @JoseHermosillaRodrigo: You can extend (subclass) constructor functions since ES1? – Bergi May 11 '16 at 01:12
  • 1
    In addition to Bergi's answers I'd like to point that altering closed functionality in JavaScript has nothing to do with AOP unless an implementation that claims to be AO provides abstraction and code-reuse levels for at least Aspect, Advice and Pointcut - http://stackoverflow.com/questions/26777671/javascript-console-output-before-and-after-method-call-with-aop/27650749#27650749 / http://stackoverflow.com/questions/11371993/sandwich-pattern-in-javascript-code/27649488#27649488 / http://stackoverflow.com/questions/2136522/can-you-alter-a-javascript-function-after-declaring-it/27533537#27533537 – Peter Seliger May 29 '16 at 14:06
  • Here is an article that explain all of this and how to achieve AOP in JS https://hackernoon.com/aspect-oriented-programming-in-javascript-es5-typescript-d751dda576d0 – k1r0s May 31 '17 at 10:09

0 Answers0