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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:39:38+00:00 2026-05-31T23:39:38+00:00

Regarding the question on SO How to overload the calling process of any function

  • 0

Regarding the question on SO How to overload the calling process of any function. The accepted answer is in fact not working as is. The problem lies in the fact the passed in variable “fn” is a string instead of a function. But if I changed fn() to NS[fn] (), it would create an infinite loop. Another way is to use a var inside the for loop to keep track of the old values of foo and bar, but it won’t work too because of closure. I ended up adding a new array to keep track of the old functions as below:

NS = {
   foo : function(){ console.log('foo'); },
   bar : function(){ console.log('bar'); }
};

// <-- new code goes here

NS.foo();
NS.bar();

// new code below, that should go after definitions but before the calls

(function(){

   var pre = function(){ console.log('pre'); },
       post = function(){ console.log('post'); };

   var temp = new Array();//Added line 1

   for (var fn in NS) {
      temp[fn] = NS[fn];//Added line 2
     //original answer: NS[fn] = (function(fn){ return function(){ pre(); fn(); post(); }; })(fn);
      NS[fn] = (function(fn){ return function(){ pre(); temp[fn](); post(); }; })(fn);
   }

})();

NS.foo();//Added line 3
NS.bar();//Added line 4

My question is: are there other ways to do similar thing? I am not talking about a caller function as given by another answer to the quoted question. It is a good one. But I am just trying to find a different approach going the same way as the above given answer. Thanks a lot.

  • 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-05-31T23:39:39+00:00Added an answer on May 31, 2026 at 11:39 pm

    You weren’t passing the function into the anonymous block, you were just passing the name of the property holding it. Pass the whole function and it should work http://jsfiddle.net/mendesjuan/bpq8j/

    (function(){
    
       var pre = function(){ console.log('pre'); },
           post = function(){ console.log('post'); };
    
       for (var propName in NS) {
          NS[propName] = (function(fn){ 
            return function(){ 
              pre(); 
              fn(); // could be fn.call(NS) if you need 'this'
              post(); 
            }; 
          })(NS[propName]);
       }
    
    })();
    

    I changed your code slightly, abstracting the idea of pre/post into a function.
    http://jsfiddle.net/mendesjuan/bpq8j/1/

    function addPrePost(fn, pre, post) {
        return function() {
          pre();
          fn();
          post();
        }
    }
    
    (function(){
       var pre = function(){ console.log('pre'); },
           post = function(){ console.log('post'); };
       for (var propName in NS) {
          NS[propName] = addPrePost(NS[propName], pre, post);
       }
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One question regarding whether the following code should yield a compiler warning or not
Possible Duplicate: Best practices regarding equals: to overload or not to overload? Does anyone
Quick question regarding __callStatic() in PHP; class Test{ public static function __callStatic($method, $arguments){ echo
In this question regarding magic numbers in arrays, paxdiablo says that any number other
Question regarding my sql database design for a project i am working on. I
A question regarding a parser. Is there any chance to catch some separators within
Quick question regarding Google Maps. I have one fully working Google Maps project and
Quick question regarding Javascript. I'm working on a Safari Extension for paring down the
A quick question regarding Events in Backbone - is there any way to define
I have question regarding the CreateWindowEx function. I have 2 windows, a main one

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.