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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:31:56+00:00 2026-06-14T23:31:56+00:00

Explanation I have a function that is passed the parameter options . This parameter

  • 0

Explanation

I have a function that is passed the parameter options. This parameter can either be an object, array or string. Depending on what the parameter is, will determine what to do.


UPDATE: I forgot to mention that, options must always end up as an object of the same structure (in other words, it must always have the default values set).

I only want to define the default values once so using procedural if statements as some of you have suggested is not my preferred solution, but I will resort to it if necessary.

I do not want to do this (if possible):

function foo(options){
    switch(typeof options){
        case 'string':
            // do something etc
        break;  
        // etc
    }
}

Example

If the parameter is an object, then extend it to set the defaults like so:

function foo(options){
    // Extend the options to apply default values
    var options = $.extend({
        bar: 'none',
        baz: []
    },options);         
}

If the parameter is a string then set the options.bar to equal the string and extend the default values (something like this):

function foo(options){
    // Set the bar property to equal the supplied string
    var options = {
        bar: options    
    };
    // Extend the options to apply default values
    options = $.extend({
        baz: []
    },options);         
}

If the parameter is an array then set the options.baz to equal the array, and extend the default values (something like this):

function foo(options){
    // Set the baz property to equal the supplied array
    var options = {
        baz: options    
    };
    // Extend the options to apply default values
    options = $.extend({
        bar: 'none'
    },options);         
}

Question

So effectively, I want to be able to supply a parameter in any format, and the function will build the same options object from what has been supplied. If the values have not be supplied then they resort to their default values.

Sorry this is so unclear, it is very hard to explain.

Additional Example

Another potential way I (jQuery) can demonstrate is by looking at a function like animate(). Notice that you can either supply:

.animate( properties [, duration] [, easing] [, complete] )

or

.animate( properties, options )

This additional example is not exactly what I am hoping to achieve but it is along the right lines

  • 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-14T23:31:57+00:00Added an answer on June 14, 2026 at 11:31 pm

    You can use various jQuery helper functions to determine the type of options:

    $.isPlainObject(options)
    $.isArray(options)
    

    and

    typeof options === "string"
    

    e.g.

    function foo(par) {
    
        // default values
        var options = {
            bar: 'none',
            baz: []
        };
    
        if ($.isPlainObject(par)) {
            $.extend(options, par);
        } else if ($.isArray(par)) {
            options.baz = par;
        } else if (typeof options === "string") {
            options.bar = par;
        }
    
        ...
     }
    

    If you intend to change any of those values, use .slice() for array copies, and the deep-copy option on $.extend() so that changes don’t affect the object that was supplied.

    UPDATED ANSWER

    For this particular case, the answer is:

    function foo(parameter){
        var options = {
            package:    'none', // Do not load any packages by default
            packageURL: false, // Do not retrieve the package details from a URL by default
            libraries:  [] // Do not load any libraries by default
        };
        // Determine the type of parameter supplied and 
        // build the options accordingly
        if($.isArray(parameter)){
            // Set libraries option
            parameter = {
                libraries: parameter    
            };
        }else if(typeof parameter === "string"){
            // Set package option
            parameter = {
                package: parameter  
            };
        }
        // Extend the parameter object to include the default values
        $.extend(options, parameter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Explanation I have a multidimensional array that is iterated over to created a categorized
I have encountered this problem today and I don't have an explanation for it.
I have read the explanation in http://en.wikipedia.org/wiki/PageRank and i understand that the page rank
I have read over this which sort of gives an explanation of when you'd
I have a homework question please help with a little explanation so that I
I have an object that Im storing in a cookie, then getting the object
I have a function that writes file to disk. Using a concurrent server, it
I have a function that taking (int * int list) list to a boolean
I have to write a recursive function sumSquares() that takes a non-negative (>= 0)
i have a function that fill up my SQLite DB with entries of a

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.