2

My angular function is defined in a service somethin like,

$delegate.isConfigurable = function (product, config) {
                if (product) {
                    ///.....
                }
                return config.getDetail();
            };

Now, on the controller, I am calling this through:

mySer.isConfigurable(null, conf);

But passing null looks a little awakard to me.

I tried calling mySer.isConfigurable(conf); but this makes conf match to first parameter of the function.

Instead, are there any other way of passing only conf ?

3 Answers 3

3

Maybe you can just switch the order that the parameters are defined?

$delegate.isConfigurable = function (config, product) {
            if (product) {
                ///.....
            }
            return config.getDetail();
        };

Then you can call just mySer.isConfigurable(conf); and product be undefined in that case, and will evaluate to false.

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

2 Comments

Switching paramaters is fine.. but any generic ways of doing this ?
I don't believe there is. It may also be confusing to see the same function called with the only param actually being a different variable when it's called in different places. I suppose you could also make 2 separate helper functions that call this isConfigurable function with different parameters.
3

If you have a function that have varying number of parameters then you can use the arguments object

Here is the documentation

The arguments object is an Array-like object corresponding to the arguments passed to a function.The arguments object is a local variable available within all functions

So your code will be

$delegate.isConfigurable = function() {
  var param = null,
    config = null;
  if (arguments && arguments.length == 2) {
    // you have param and config
    param = arguments[0];
    config = arguments[1];
  } else if (arguments && arguments.length == 1) {
    config = arguments[0];
  }
  return config.getDetail();
};

And you can call this by one or two or n number of param

mySer.isConfigurable(conf)
mySer.isConfigurable(param,conf)
mySer.isConfigurable(param1,param2,conf)

Comments

3

If you don't want to use null then you need to interchange your function parameter like bellow.

$delegate.isConfigurable = function (config, product) {
                if (product) {
                    ///.....
                }
                return config.getDetail();
            };

And now if you call that function passing one parameter that will be the value of config and product parameter will be passed as undefined.

 mySer.isConfigurable(conf);

2 Comments

Switching paramaters is fine.. but any generic ways of doing this ?
@nowhewhomustnotbenamed. Yes, this is how it is usually done. Mandatory params go first, exactly to avoid the issue you're having. Otherwise there should be tedious parsing of arguments, as the other answer explains.

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.