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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:21:01+00:00 2026-05-23T08:21:01+00:00

what would be an efficient (and userfriendly) way to realize the following in javascript/jQuery?

  • 0

what would be an efficient (and userfriendly) way to realize the following in javascript/jQuery?

Depeding on the mime-type of a file, a callback should be executed. The callback is defined by the developer by providing a mime-type description with optional wildcards (e.g. text/javascript, text/*, */*) and the callback itself. There must be a specified order in which the declared types are matched against the given mime-type of the file. For example must text/plain have a higher priority than text/*.

Here some ideas:

Using a simple object

var handler = {
 'text/javascript' : callback0,
 'text/*' : callback1
}

This would be the most intuitive solution, but the ordering is not guaranteed.

Maintain two lists

var order = ['text/javascript', 'text/*'];
var handler = [callback0, callback1];

This is would be hard to maintain if there are more than two or three types and you are using anonymous functions as callbacks.

Adding an index by wrapping the callback into an object

var handler = {
  'text/javascript' : {index: 0, callback0},
  'text/*' : {index: 1, callback1}
};

... change thousands of index-properties when inserting an item at the beginning.

Using an array of arrays

var handler = [
  ['text/javascript', callback0],
  ['text/*', callback1]
];

This might me more userfriendly than the others, but there is no direct access for known mime-types without iterating over the elements (this would be nice-to-have).

So there are some ways to do the thing I want, but what would be the right way (and why)? Maybe someone has a pattern?

Best,
Hacksteak

  • 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-23T08:21:01+00:00Added an answer on May 23, 2026 at 8:21 am

    I would use the ‘simple object’ solution AND the order array form the ‘maintain two lists’ solution.

    Iterate through the order array with a block of code that uses the handler simple object to do something that either breaks the loop or continues to the next loop iteration.

    EDIT TO RESPOND TO A COMMENT

    I agree with your comment. I would do something like this to make it just one variable:

    var handlers = {
      handler: {
        'text/javascript': callback0,
        'text/*': callback1
      },
      order: ["text/javascript", "text/*"]
    };
    

    Although I would pick better names for the handlers variable and/or the handlers.handler property.

    ANOTHER RESPONSE TO ANOTHER COMMENT

    Maybe you should just modify handlers.handler and handlers.order one-mime-type-at-a-time:

    var handlers = { handler: {}, order: [] }; // initialize as empty
    
    // add 'text/javascript' mime type
    handlers['text/javascript'] = callback0;
    handlers.order.push('text/javascript');
    
    // add 'text/*' mime type
    handlers['text/*'] = callback1;
    handlers.order.push('text/*');
    

    That method fills a little repetitive, but should be easy to maintain in the future. If you want, you can write a function that adds a new property to handlers.handler and appends a new mime-type to handlers.order.

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

Sidebar

Related Questions

what would be the efficient way of saving the following array using php (cakephp)?
What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient data type to store a UUID/GUID in databases
What would be the most efficient method of reading a text file into a
What would be the most efficient way of counting the number of times a
In SQL Server would a varbinary(16) be the most efficient way of storing an
What would be a efficient way to generate all possible IP v4 addresses? other
Because an anonymous type is readonly anyway, is would be more efficient if they
What would be an efficient and the right way to implement this expression? {'a':
How much less efficient would it be to store some fields as a BigDecimal

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.