In one of my site's functions (myFunction), I had four variables that I needed passed to another function (an Ajax function that has a general format - function GeneralFunction ('id1', 'id2', param1, param2, param3) ).

At this point, I need to keep this general format, but myFunction had too many parameters to pass to GeneralFunction. So I concatenated as follows:

function myFunction(a, b, c, d) {
    //Lots of code then
    var param3 = c + "," + d;
    GeneralFunction('id1', 'id2', a, b, param3);
}

I then explode param3 and process what was c and d in the php file to which GeneralFunction passes param3....this works fine.

However, my question is as follows...is the following code always acceptable? I thought I would save a step by rewriting myFunction as:

function myFunction(a, b, c, d) {
    //Lots of code then
    GeneralFunction('id1', 'id2', a, b, c + "," + d);
}

c and d are concatenated as they are passed to GeneralFunction. Although this worked as intended, and the online lint js checker I use (http://www.javascriptlint.com/online_lint.php) did not detect any errors, is concatenating (or performing any kind of operation, arithmetic, concatenation, etc.) and passing an argument - simultaneously - a generally acceptable protocol?

I did not find much in my google search, and just because it worked once does not in my opinion mean it is always going to work!

有帮助吗?

解决方案 3

This is honestly not very advisable. Not only is the code itself rather unhygienic to look at, but you'll lose type information for the extra parameters. Consider, for example, if c and d were a bool and number, respectively:

var c = true,
    d = 3;

var combined = c + ',' + d; // "true,3"

combined.split(','); // ["true", "3"]

Notice that you now have two strings ("true", and "3"), rather than your initial values.

There are other issues as well. For example, if either c or d is a string with commas in it, you'll end up with unexpected results on the other side:

var c = 'hello,world',
    d = 'foo,bar';

var combined = c + ',' + d; // "hello,world,foo,bar"

combined.split(','); // ["hello", "world", "foo", "bar"]

At this point, it's impossible to tell which "parts" were originally from c vs. from d. So now you have to introduce some escaping for commas... yuck.

The ideal solution for this would be to simply change your strict 4-parameter requirement. If this isn't possible, however, I would instead recommend passing extra variables in an array:

function myFunction(a, b, c, d) {
    //Lots of code then
    GeneralFunction('id1', 'id2', a, b, [c, d]);
}

With this approach, you'll have no loss of type info, and it'll be easier to retrieve the values on the other side.

其他提示

Is performing any kind of operation (arithmetic, concatenation, etc.) and passing an argument - simultaneously - a generally acceptable protocol?

Yes, totally acceptable. It's quite rare actually that only plain variables are passed to a function, most often the arguments consist of arbitrary expressions. Notice they don't happen "simultaneously", but the arguments are evaluated left-to-right and then their result values are passed to the function.

<html>
    <head>
        <title></title>
    </head>
    <script>
        function fun1(a,b,c,d)
        {
            alert(a+" "+b+" "+c+" "+d);
            var demo=c+","+d;
            fun2(a,b,demo);
        }
        function fun2(a,b,d)
        {
            alert(a+" "+b+" "+d);
        }
    </script>

    <body>

        <a href="javascript:void(0)" onclick="fun1(1,2,3,4)">Call</a>
    </body>
</html>

If "," (Comma) not work in concatenation use # or other sign.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top