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 7888105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:45:06+00:00 2026-06-03T05:45:06+00:00

What I want to do is the following : I have a function that

  • 0

What I want to do is the following :

I have a function that alert something :

myfunction = function(foobar) { 
                 alert(foobar); 
             };

Now I want to decorate it so that :

decorate = function(callback) { 
              return function(foobar) { 
                  callback(foobar); 
                  console.log(foobar); 
           }; 
};

So then I can write :

myfunction = decorate(myfunction);

And then myfunction will do the normal + log in the console.

How can I make it works with Javascript ?

  • 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-03T05:45:07+00:00Added an answer on June 3, 2026 at 5:45 am

    Yes, you can. And in fact, you have, your implementation works perfectly: Live example | source

    var myfunction = function(foobar) { alert(foobar); };
    
    var decorate = function(callback) { return function(foobar) { callback(foobar); console.log(foobar); }; };
    
    var result = decorate(myfunction);
    
    result("Hi there");
    

    I would recommend using function declarations rather than function expressions, though:

    function myfunction(foobar) {
        alert(foobar);
    }
    
    function decorate(callback) {
        return function(foobar) {
            callback(foobar);
            console.log(foobar);
        };
    }
    
    var result = decorate(myfunction);
    
    result("Hi there");
    

    And if you want to create a more generic version, look at using apply (MDN | spec) and the arguments pseudo-array (MDN | spec): Live example | source

    function decorate(original, wrapper, context) {
        return function() {
            try {
                original.apply(this, arguments);
            }
            catch (e) {
            }
            try {
                wrapper.apply(context || this, arguments);
            }
            catch (e) {
            }
        };
    }
    
    function myFunction(arg1, arg2) {
        alert("arg1 = " + arg1 + ", arg2 = " + arg2);
    }
    
    var newFunction = decorate(myFunction, function(arg1, arg2) {
        console.log("arg1 = " + arg1 + ", arg2 = " + arg2);
    });
    
    newFunction(1, 2);
    

    That version does a couple of things:

    1. Lets you supply the callback as an argument to one central decorate function.

    2. Lets you optionally supply a “context” (this value) to use when calling the callback.

    3. Preserves the value of this when calling both the original and (if you don’t supply context) the callback.

    …which an be handy when decorating object functions (sometimes called methods).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function that I only want to run inside <div id=imgWrapper>
Eg. I have following delegate method I want to use as a callback function
I have the following function that I want to run on page load and
i have a variable like the following and i want a function to only
I have following two arrays. I want the difference between these two arrays. That
I have the following function in javascript that calls the ajax which calls the
I have the following function that is pulling data from a database. The ajax
I'm have a function that accepts two parameters: filter_field and filter_value. I want to
I have the following situation: I have a certain function that runs a loop
I have following LINQ statement and I want to rewrite it using extension methods.

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.