Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:13:13+00:00 2026-06-14T15:13:13+00:00

I want to do an AOP-like ‘before’ functionality for JavaScript functions. So I looked

  • 0

I want to do an AOP-like ‘before’ functionality for JavaScript functions.

So I looked for existing solution and found the aop-plugin for jQuery. Unfortunately the plugin simply wraps the given function. So any object that points to the original function before enhancing the function, still points to the original function. Calling the function from these objects calls the not enhanced version of the function.

Instead of wrapping functions with other functions, I want to change the function object itself, so previous references do not cause unanticipated behaviour. So I got something like this:

(function(){}).constructor.prototype.before = function(beforeFunction){
    this.beforeFunction = beforeFunction;
};

/* some magic should happen here: enhance call? */

function logFoo() {
    console.log("Foo");
};
function logBar() {
    console.log("Bar");
};

logFoo(); // logs "Foo"

logFoo.before(logBar);

logFoo(); // should log "Bar", then "Foo"

So the question is, how do I get the beforeFunction invoked, when the enhanced function is called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:13:13+00:00Added an answer on June 14, 2026 at 3:13 pm

    The body, parameters and referencing environment of a function are immutable so you cannot enhance a function, you can only make new functions.

    You could make your function enhanceable from the beginning like this:

    Function.prototype.enhanceable = function() {
        var original = this,
            enhanced = [],
            ret = function() {
                for( var i = 0; i < enhanced.length; ++i ) {
                    enhanced[i].apply( this, arguments );
                }
    
                return original.apply( this, arguments );
            };
    
        ret.before = function( fn ) {
            enhanced.push( fn );
        };
    
        return ret;
    };
    

    Then:

    var logFoo = function() {
         console.log( "foo" );
    }.enhanceable();
    
    var logBar = function() {
         console.log( "bar" );
    };
    
    logFoo(); //foo
    
    logFoo.before( logBar );
    
    logFoo(); //logs bar, then foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use AOP to apply cache functionality to all of method of
I am working on profiling a legacy application using Spring AOP and I want
Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I want to use Spring AOP in my spring application. While creating AOP proxy
I don't want to use something like this : Logger.getLogger(MyClass.class).info(Info message); each time I
I am doing an AOP kind of layer and I would like to return
I want to use an AOP framework, but I have two constraints. Any framework
For a given class I would like to have tracing functionality i.e. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.