www.webdeveloper.com
Results 1 to 5 of 5

Thread: How can I create argument defaults for a function?

  1. #1
    Join Date
    Dec 2008
    Posts
    488

    Question How can I create argument defaults for a function?

    How can I create argument defaults for a function?

    I have this function (the purpose of the lines I included were to set defaults if the user forget to input something simply to prevent an error. I realize there is some lack of logic here):
    Code:
    addHSLink = function (hsElem, hsUrl, hsType, hsTitle, hsWidth, hsHeight) { //hsElem[string], hsUrl[string], hsType[string], hsTitle[string], hsWidth[string/number], hsHeight[string/number]
        var jQElem = $(hsElem);
        if (typeof hsUrl !== 'string') {
            hsUrl = 'http://someurl.com';
        }
        if (typeof hsTitle !== 'string') {
            hsTitle = '';
        }
        if ((typeof hsWidth !== 'string' && isNaN(parseFloat(hsWidth))) || hsWidth !== 'number') {
            hsWidth = 800;
        }
        if ((typeof hsHeight !== 'string' && isNaN(parseFloat(hsHeight))) || hsHeight !== 'number') {
            hsHeight = 600;
        }
    
        //Some other code
    }
    I would like to save some programming space and time by finding another way to set defaults

    I saw somewhere that you could set a default in the arguments list
    Code:
    function someFunction(aVariable = false) {
    //some code
    }
    but when I try to do this with all my arguments, I get the error:
    "Missing ) after formal parameters"
    Does that make sense

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Something like this is probably the best you can do.

    Code:
        // Defaults
        hsUrl = hsUrl || 'http://someurl.com';
        hsTitle = hsTitle || '';
        hsWidth = hsWidth || 800;
        hsHeight = hsHeight || 600;
    I saw somewhere that you could set a default in the arguments list
    That was probably PHP code you were looking at. JavaScript doesn't do that.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    Too many arguments to keep track of that way-

    Code:
    addHSLink= function(elem, atts){
        jQ= $(elem), atts= atts || {};
        var props={ href: 'http://someurl.com', width:800, height:600}; // defaults
        for(var p in atts) props[p]= atts[p];
            
        //Some other code, using the defaults (if you do not override them)
        // and any other properties you care to pass in the function call
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    488
    Cool thanks. I realized later that I needed to pass an object, so I decided to do it this way.
    Code:
    function addHSLink(hsOptions) {
        var objDefaults = {
            "elem" : "",
            "url" : false,
            "type" : image,
            "title" : "",
            "width" : 800,
            "height" : 600
        }, options;
        for (options in hsOptions) {
            objDefauls[i] = hsOptions[i];
        }
        //some other stuff
    }

  5. #5
    Join Date
    Dec 2008
    Posts
    488
    Heh I see we posted at pretty much the same time. Thanks mrhoo. Do you think it's better to initialize the count for the for loop inside the loop's initialization params? I've been taught to initialize all instance variables in a single var line at the top of the function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles