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

  • Home
  • SEARCH
  • 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 688975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:17:05+00:00 2026-05-14T02:17:05+00:00

I’ve got a prototype having a method I can add callbacks with: /* *

  • 0

I’ve got a prototype having a method I can add callbacks with:

/*
 * Add a callback function that is invoked on every element submitted and must return a data object.
 * May be used as well for transmitting static data.
 * 
 * The callback function is supposed to expect a jQuery element as single parameter 
 * and must return a data object (for additional data to be sent along with the one already given upon initialization).
 * Adding multiple callback functions results in those functions being invoked in the same order as they were added.
 * 
 * 1) Therefore subsequent assignments to the same key in the data array override those that were performed by a previous callback function.
 * 2) If data_arg = true is given, the data returned by the callback function that was previously called is given to the new_callback as 2nd argument, so it can be manipulated.
 *    However, even if it isn't, the unchanged data must be returned anyway to have any effect.
 */

this.add_data_callback = function(new_callback, data_arg) {

    if(this.data_callback) {
        old_callback = this.data_callback;

        if(!data_arg) {
            //alert('add it');
            //alert(old_callback);
            //alert(new_callback);

            this.data_callback = function(element) {
                //alert('do it');
                //alert(old_callback);
                //alert(new_callback);
                return jQuery.extend(old_callback(element), new_callback(element));
            };
        }
        else {
            this.data_callback = function(element, data) {
                return new_callback(element, old_callback(element));
            };
        }
    }
    else {
        //alert('add the first');
        //alert(new_callback);
        this.data_callback = new_callback;
    }
};

(Please ignore the else part for data_arg = true as it’s not relevant.)

In my concrete case I’m adding three callback functions. If this.data_callback() is finally invoked for an element, however, it results in the whole thing looping infinitely.
In my attempt to track the bug down, the alerts above (yeah, I know that there’re debugging tools for that but it was way more comfortable like that) delivered the following insight into the issue:

  1. The anonymous function / closure (the one that contains jQuery.extend()) is called.
    new_callback contains the 3rd callback function.
    old_callback contains another…
  2. anonymous function which is called.
    new_callback contains the 2nd callback function.
    old_callback should contain the 1st callback function, but actually it’s yet another…
  3. anonymous callback which is called.
    new_callback contains the 2nd callback function again.
    old_callback contains the anonymous function
  4. …

Now, what do I miss? Is it some strange closure magic or just some obvious bug I’m apparently unable to see?

Thanks in advance!

  • 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-14T02:17:05+00:00Added an answer on May 14, 2026 at 2:17 am

    Why isn’t old_callback defined with var?

    var old_callback = this.data_callback;
    

    As it is, it’s a global variable. Maybe it’s declared elsewhere but it still looks suspicious to me.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
We're building an app, our first using Rails 3, and we're having to build
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.