3

この行を最小化するために、クリエイティブな方法/パッチを見つけようとしています:

 if(myParam == '1' || myParam == 'a' || myParam == '*' || myParam == '@' || myParam == undefined || myParam == null || myParam == ' ')
    {...
    }

1 つの解決策は、indexOf を使用することですが、クロス ブラウザーではありません (indexOf のように動作する独自の関数を作成できますが、そうしたくありません)。

だから私は演算子を試しました in

ただし、in 演算子は、オブジェクト プロパティの名前インデックスのみを扱います。

だから私はこれを試しました(オブジェクトのプロパティ):

   if(window.lala in {
    '*': 0,
    'a': 0,
    '@': 0,
    ' ': 0,
    undefined: 0,
    null: 0
}) alert('1')

そして私はそれが働いていると思います。

2 質問してください:

question #1

undefinedプロパティ名が [ ] または [ ' '] または [ ]であっても安全nullですか? それは常に動作しますか?

question #2 これを行うための他の解決策はありますか?

(ケース/スイッチも可能ですよね...)

4

3 に答える 3

1

What you have won't work for undefined. You can have a property named "undefined", but that's a string name, not the actual undefined value. You may get a match via type conversion, but it will also match if you have a string named "undefined" too which is not something you want.

I would suggest a more explicit test that uses === to avoid any type conversions. There are several ways to do it that are slightly less verbose than what you had:

if (myParam === null || myParam === undefined || 
    (myParam && myParam.length == 1 && "1a*@ ".indexOf(myParam) !== -1) {
   // code here
}

Or you can make an array and your own cross browser function (or use a polyfill for Array.indexOf() to search an array which can hold all the values:

function inArray(val, array) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] === val) {
            return(true);
        }
    }
    return(false);
}

var testValues = [null, undefined, "1", "a", "*", "@", " "];
if (inArray(window.lala, array)) {
    // your code here
}

You can use the object form you were experimenting with and just use explicit tests for null and undefined, but I think the string.indexOf() suggestion in my first proposal is simpler. Anyway, here's a safe form using the object

if (myParam === null || myParam === undefined || 
    (myParam && (myParam in {"*":0, "1":0, "a":0, "@":0, " ":0})) {
   // code here
}

My recommendation would be the first option as I think it's the cleanest and it's also really easy to extend by just adding more chars to the test string.

于 2012-10-11T07:37:45.733 に答える
1

[undefined]プロパティ名がor [' '] orであっても安全[null]ですか? それは常に機能しますか?

0すべてのプロパティ名は文字列に変換されます。つまり、数値と文字列を区別できず'0'、値null( undefined) と文字列'null'( 'undefined') を区別できません。

あなたの場合、数値を数値文字列として扱うことはおそらく問題ありませundefinednull

if(value == null || value in exclude) {
    // ...
}

value == nullはまたはです。true_valuenullundefined

于 2012-10-11T07:39:36.370 に答える
0

1: オブジェクトの名前として 'undefined' と 'null' を使用することをお勧めします。これは、for in ループで処理するときに、プロパティの文字列表現をチェックするためです (以下を実行するとチェックできます)。

 for(prop in obj) typeof(prop) //this will be a string

2: 値を配列に格納して再帰を行うこともできます。

 var vals = ['*', 'a', '@', ' ', undefined, null], param = '@', i = 0;

function checkVals(param, vals, i) {
    if (vals[i] && param === vals[i]) {
        console.log(1);
        return;
    } 

    checkVals(param, vals, i + 1);
}

checkVals(param, vals, i);
于 2012-10-11T07:49:30.227 に答える