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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:37:59+00:00 2026-05-23T19:37:59+00:00

I have code in the following form in which the user may specify a

  • 0

I have code in the following form in which the user may specify a callback which will be called at a later time:

var _deferred = [];

var deferred = function(callback) {
    _deferred.push(callback);
}

var doDeferred = function() {
    for(var i = 0, max = _deferred.length; i < max; i++) {
        _deferred[i].call();
    }
}

for(var i = 0; i < 5; i++) {
    deferred(function() {
        console.log("Some deferred stuff");
    });
}

doDeferred();

I would like to recognize that the callback specified to deferred() is an anonymous function resolving to the same origin, and only allow it to be added once. Aka in the bottom for loop it would throw an exception when i = 1.

Like:

var deferred = function(callback) {
        if(_deferred.indexOf(callback) !== -1) {
                throw "Already added!";
        }
        _deferred.push(callback);
}

I can think of many ways of doing this by adding a “key”, but I’m wondering if I can use something along the lines of Function.caller to create a “source hash”?

Is there a solution for this out there already that I’m not seeing? I’d really prefer to accept this burden onto myself rather than push it out to the caller of deferred and have them provide some sort of unique id.

EDIT:

To clarify any confusion.

Yes each call to deferred is with a unique function object, has its own closure, etc. Therefore my indexOf will always fail. That is not a point of confusion.

The question is that these anonymous functions are declared in the same place, they are the same code, how can I determine that? I’m looking to determine declarative equality, not instance equality. I was thinking I could somehow create a hash based on caller of deferred…

CONCLUSION:

Thanks guys, it seems like there is no really elegant solution here. The toString method is not definitive (different functions with same body will test equally as strings) – and is just ugly. I’m going to put the burden on the caller.

  • 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-23T19:37:59+00:00Added an answer on May 23, 2026 at 7:37 pm

    The thing is, in the loop at the bottom, they are different functions, so in all fairness they should both be included (and honestly, there is no guarantee that the values from both functions won’t be different depending on the variables present at the moment). I’m also not sure that ‘unique functions only’ is something which people expect, so it might cause a good deal of “debugging”

    This isn’t something which is required of ECMAScript, but Function.toString() will generally return its internal structure. So you probably want:

    var ids = [] // separate ID takes up slightly more space, but lookup should 
                 // be faster.
    var deferred = function(callback) {
            var cbs = callback.toString() // or String(callback)
            if(ids.indexOf( cbs ) !== -1)
            {
                    throw "Already added!";
            }
            ids.push( cbs )
            _deferred.push(callback);
    }
    

    If you’re willing to use a for… in loop:

    var _deferred = {}
    var deferred = function(callback) {
            var cbs = callback.toString() // or String(callback)
            if( _deferred[ cbs] )
            {
                    throw "Already added!";
            }
            _deferred[ cbs] = callback;
    }
    
    // caution, this executes in arbitrary order.
    var doDeferred = function() {
        for(var i in _deferred) {
            _deferred[i].call();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control which contains the following code: <form id=CurrencyForm method=post runat=server>
I need to have a checkbox which ajax-submits a form. The following code throws
I have code which will draw a graph that scales if the user attempts
I have the following code which will check to see if the following fields
I have the following code, which is supposed to create a form on the
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
I have the following code: // Form the continuities list string[] continuities = new
I have the following form code: # forms.py class SomeForm(forms.Form): hello = forms.CharField(max_length=40) world
I have the following code I use to insert form data into a single
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input

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.