最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Pass one parameter in multiple arguments function in javascript - Stack Overflow

programmeradmin5浏览0评论

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 ?

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 ?

Share Improve this question edited May 6, 2017 at 15:49 Upalr 2,1482 gold badges25 silver badges33 bronze badges asked May 6, 2017 at 2:32 now he who must not be named.now he who must not be named. 9,39630 gold badges87 silver badges155 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

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)

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);
发布评论

评论列表(0)

  1. 暂无评论