如何最好地确定参数不会被发送到JavaScript函数 [英] How best to determine if an argument is not sent to the JavaScript function

查看:188
本文介绍了如何最好地确定参数不会被发送到JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到2种方法用于确定一个参数已经传递给JavaScript函数。我想知道,如果一个方法比其他更好的或者一个就是不好用?

 功能测试(参数1,参数2){
      如果(Test.arguments.length == 1)参数2 ='嗒嗒';      警报(参数2);
 } 测试(测试);

或者

 功能测试(参数1,参数2){
      参数2 =参数|| '嗒嗒';      警报(参数2);
 } 测试(测试);

据我所知,他们都导致同样的事情,但我之前只在生产中使用的第一个。

按<一提到另一种选择href=\"http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function#411368\">Tom:

 功能测试(参数1,参数2){
    如果(参数2 === NULL){
        参数2 ='嗒嗒';
    }    警报(参数2);
}

根据娟的评论,这将是更好汤姆的建议更改为:

 功能测试(参数1,参数2){
    如果(参数2 ===未定义){
        参数2 ='嗒嗒';
    }    警报(参数2);
}


解决方案

有几种不同的方法来检查,如果一个参数传递给函数。除了这两个你在(原)中提到的问题 - 检查与arguments.length 或使用 || 运营商提供默认值 - 人们也可以通过显式地参数2 ===未定义或<$ c检查论据未定义 $ C>的typeof参数2 ===未定义如果有一个偏执狂(见注释)。

使用 || 运营商已经成为标准做法 - 所有时尚的年轻人做到这一点 - 但要小心:如果参数的计算结果为<$默认值将被触发C $ C>假,这意味着它实际上可能是未定义 0 (或任何其他为这布尔(...)收益)。

所以现在的问题是,当要使用的检查,因为它们都产生稍有不同的结果。

检查与arguments.length 表现出最正确的行为,但它可能不是如果有多个可选的参数是可行的。

测试了未定义是下一个最好的 - 它只有'失败'如果函数显式调用与未定义值,这在所有的情形产生应该被视为相同的方式省略参数。

使用该 || 运营商可能会引发即使提供有效的参数的默认值的使用。在另一方面,其行为实际上可能需要的。

要总结:只有当你知道自己在做什么,用它

在我看来,使用 || 也去,如果有多个可选参数的方式,一个不希望传递一个对象文本作为一种解决方法为命名参数。

另一种很好的方式使用,以提供默认值与arguments.length 可通过switch语句的标签掉落:

 功能测试(requiredArg,optionalArg1,optionalArg2,optionalArg3){
    开关(与arguments.length){
        案例1:optionalArg1 ='缺省1';
        案例2:optionalArg2 ='默认2';
        案例3:optionalArg3 ='default3';
        案例4:打破;
        默认:抛出新的错误('非法参数计数)
    }
    // 做东西
}

这有不利的一面是,程序员的意图是不是(视觉)明显,使用幻数;因此,它可能是容易出错。

I have now seen 2 methods for determining if an argument has been passed to a JavaScript function. I'm wondering if one method is better than the other or if one is just bad to use?

 function Test(argument1, argument2) {
      if (Test.arguments.length == 1) argument2 = 'blah';

      alert(argument2);
 }

 Test('test');

Or

 function Test(argument1, argument2) {
      argument2 = argument2 || 'blah';

      alert(argument2);
 }

 Test('test');

As far as I can tell, they both result in the same thing, but I've only used the first one before in production.

Another Option as mentioned by Tom:

function Test(argument1, argument2) {
    if(argument2 === null) {
        argument2 = 'blah';
    }

    alert(argument2);
}

As per Juan's comment, it would be better to change Tom's suggestion to:

function Test(argument1, argument2) {
    if(argument2 === undefined) {
        argument2 = 'blah';
    }

    alert(argument2);
}

解决方案

There are several different ways to check if an argument was passed to a function. In addition to the two you mentioned in your (original) question - checking arguments.length or using the || operator to provide default values - one can also explicitly check the arguments for undefined via argument2 === undefined or typeof argument2 === 'undefined' if one is paranoid (see comments).

Using the || operator has become standard practice - all the cool kids do it - but be careful: The default value will be triggered if the argument evaluates to false, which means it might actually be undefined, null, false, 0, '' (or anything else for which Boolean(...) returns false).

So the question is when to use which check, as they all yield slightly different results.

Checking arguments.length exhibits the 'most correct' behaviour, but it might not be feasible if there's more than one optional argument.

The test for undefined is next 'best' - it only 'fails' if the function is explicitly called with an undefined value, which in all likelyhood should be treated the same way as omitting the argument.

The use of the || operator might trigger usage of the default value even if a valid argument is provided. On the other hand, its behaviour might actually be desired.

To summarize: Only use it if you know what you're doing!

In my opinion, using || is also the way to go if there's more than one optional argument and one doesn't want to pass an object literal as a workaround for named parameters.

Another nice way to provide default values using arguments.length is possible by falling through the labels of a switch statement:

function test(requiredArg, optionalArg1, optionalArg2, optionalArg3) {
    switch(arguments.length) {
        case 1: optionalArg1 = 'default1';
        case 2: optionalArg2 = 'default2';
        case 3: optionalArg3 = 'default3';
        case 4: break;
        default: throw new Error('illegal argument count')
    }
    // do stuff
}

This has the downside that the programmer's intention is not (visually) obvious and uses 'magic numbers'; it is therefore possibly error prone.

这篇关于如何最好地确定参数不会被发送到JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆