开发者

How String.concat() implements in javascript?

In function .concat(), I can pass an arbitrary number of arguments to it.

I understand function overloading in C++, but I don't know how implement a function with unknown number of arguments in JavaScript.

H开发者_高级运维ow do I implement an arbitrary number of arguments to a function?


In javascript, you would use the built in parameter called "arguments" which is an array of all the arguments passed to the function. You can obtain it's length with arguments.length and each value from the array arguments[0], arguments[1], etc... Every function has this built in variable that you can use.

For example, a function to concatenate all strings passed to it.

function concatAll() {
    var str;
    for (var i = 0 ; i < arguments.length; i++) {
        str += arguments[i];
    }
    return(str);
}

var f = concatAll("abc", "def", "ghi");   // "abcdefghi"


You can do this using the arguments object. See the examples and documentation here: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments


Like this -

function f()
{
    var i;

    for(i=0; i<arguments.length; i++)
    {
        alert( (i+1) +  "th argument: " + arguments[i]);
    }
}

All the functions in javascript has a built-in parameter called arguments which is an array containing all the function arguments passed to the function. Just iterate over this array and you will be able to access all the arguments of a function.

As an example, once I've written a function which is used to enable/disable certain button if some specific fields were not empty. I wrote this function this way -

function toggleButton()        // I used jquery inside this function
{
    var i;
    var last = arguments.length-1;

    for(i=0; i<last; i++)
    {
        if( $.trim($(arguments[i]).val()) === "" )
            return false;
    }

    $(arguments[last]).toggle();

    return true;
}

and called this function like this -

toggleButton("#idOfFirstField", "#idOfSecondField", "#idOfButtonToToggle");

or like this -

toggleButton("#idOfFirstField", "#idOfSecondField", "#idOfThirdField", "#idOfButtonToToggle");

so in both the cases, I was passing variable number of field ids to the function and it checked that if these fields were empty. If all of them contained some value, then it toggled the visibility of the button.


Like this - use the arguments object all functions have available :

function someFunction() {
  for (var i=0,n=arguments.length;i<n;i++) {
    // do something with arguments[i];
  }
}


You can use the arguments array to access parameters that are not formally declared inside the function:

function printArguments() {
  for (i = 0; i < printArguments.arguments.length; i++)
    document.writeln(printArguments.arguments[i] + '<br />');
}

printArguments(1, 2, 3, 'etc');

Source: http://www.irt.org/articles/js008/


Any javascript function can have an arbitrary number of arguments. If function execution depends on the number or specific qualities of it's arguments, you'll have to check the arguments object, which can be iterated like an 'Arraylike' object, as others have shown.

In some cases it may be handy to convert the arguments to a real array, using something like
var args = Array.prototoype.slice(arguments).

Here's a blog entry from John Resigs page on method overloading that may interest you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜