1

I have a function with 2 parameters, it should work whether a the 2nd parameter is assigned or not in the bracket. Basically, if it's assigned then do something if not do something else or just don't bother about it.

vf.showHide = function (trigger, target) {
    var $trigger = $(trigger),
        trigParent = $trigger.parent(),
        trigDataView = $trigger.data('view'),
        numShown = $trigger.data('showalways'),
        basketSubtotalElem = $('.subtotal .monthlyCost span.price, .subtotal .oneOffCost span.price, .subtotal label h3, .vat *');

    target = target || null; // This is the 2nd parameter but I don't know if this right...

    trigParent.delegate(trigger, 'click', function (e) {
        var elem = $(this);

        target = $(elem.attr('href'));

        e.preventDefault();

        if (trigDataView === 'showhide') {
            if($('.filterBlock')){
                if (target.is(':visible')) {
                    target.hide();
                    elem.find('span').removeClass('minus').addClass('plus');
                } else {
                    target.show();
                    elem.find('span').removeClass('plus').addClass('minus');
                }
            } 
        }
    });
}

So if the function is called like this: vf.showHide('a', 'div') it works and if it's called with 1 parameter like this: vf.showHide('a') it's should still works and error is thrown.

Many thanks

3
  • What are you trying to do exactly? What do you want the function to do if target is undefined what do you mean by "if not do something else or just don't bother about it" Commented Nov 15, 2011 at 15:28
  • @Keith.Abramo: Basically I want the function to perform a default work if the 2nd param is not assigned and if it's assigned then use that param. Commented Nov 15, 2011 at 15:31
  • The second parameter doesn't appear to be used anywhere... You use it in the line target = target || null; but the next time it appears you are assigning somethign to it (target = $(elem.attr('href'));). I think you need to explain better what you are wanting to do. In your above comment it isn't clear what the default work is and if its assigned then what you are wanting to use it for... Commented Nov 15, 2011 at 15:46

2 Answers 2

2

When you invoke a function, if you pass fewer parameters than expected, the parameters you omit are given the undefined value. So in your case:

vf.showHide = function(trigger, target) {
    if (target === undefined) {
        //target parameter is not passed any value or passed undefined value
        //add code to process here, e.g. assign target a default value
    }
}

target = target || null: if target is evaluated to false, it's assigned to null. Take notice that empty string, zero number (0), NaN, undefined, null, false are evaluated to false. So please be careful to write code like that.

Sign up to request clarification or add additional context in comments.

Comments

1

target = target || null will work.

What you are doing here is declare an local variable within the function's scope.

Within each function, a local variable corresponding to the the name of the parameters are created to hold the passed in value.

If the parameters are not passed in, it will remain as 'undefined' local variable.

function (a, b) {
    //a, b are declared.
} 

what target = target || null does is just assign a value to an declared local variable it use the || expression:

The value of of || expression is determined by the first operands return true.

true || 2 will be valued as true

false || 2 will be valued as 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.