Function's arguments

Discussion in 'Javascript' started by sfeher@gmail.com, Jul 19, 2006.

  1. Guest

    Hi All,

    Is there a way to preserve the arguments across functions?

    I have:

    <script>
    function myFirstFunction()
    {
    // arguments[0] = 'param1'
    // arguments[1] = 'param2'
    //...
    notMyFunction(arguments);
    }

    function notMyFunction()
    {
    // arguments[0][0] = 'param1'
    // arguments[0][1] = 'param2'
    //...
    mySecondFunction(arguments);
    }

    function mySecondFunction(params)
    {
    // params[0][0] = 'param1'
    // params[0][1] = 'param2'
    //...
    }

    myFirstFunction('param1', 'param2', 'param3');

    </script>

    What I'd like to receive is params[1] instead of params[0][1] since
    params[1] never holds anything.

    I cannot change notMyFunction() so my question is: is there a way of
    calling notMyFunction() from myFirstFunction() and pass the arguments
    expanded ?! So far my guess is that I can't do this but it would be
    nice if there was something like it.

    Regards,
    Sebastian
    , Jul 19, 2006
    #1
    1. Advertising

  2. wrote:


    > Is there a way to preserve the arguments across functions?
    >
    > I have:
    >
    > <script>
    > function myFirstFunction()
    > {
    > // arguments[0] = 'param1'
    > // arguments[1] = 'param2'
    > //...
    > notMyFunction(arguments);


    You want the apply method of functions e.g.
    notMyFunction.apply(this, arguments);
    see
    <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply>


    IE 5 with JScript 5 does not have that however.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 19, 2006
    #2
    1. Advertising

  3. Sevinfooter Guest

    you could initialize the argument variables outside of any functions,
    which will make them global.

    var arguments = new Array;
    function ....
    Sevinfooter, Jul 19, 2006
    #3
  4. Sevinfooter Guest

    you could initialize the argument variables outside of any functions,
    which will make them global.

    var arguments = new Array;
    function ....
    Sevinfooter, Jul 19, 2006
    #4
  5. Guest

    Martin Honnen wrote:

    > You want the apply method of functions e.g.
    > notMyFunction.apply(this, arguments);
    > see
    > <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply>
    >
    >
    > IE 5 with JScript 5 does not have that however.


    Martin, many thanks!, This is what I was looking for.

    For IE6/apply I found this post by Laurens van den Oever on quirksmode:
    http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
    #61.

    // Ancient browser compatibility
    if (!Function.prototype.apply)
    {
    Function.prototype.apply = function (obj, args)
    {
    obj.___fn = this;
    var str = "";
    for (var i = 0; i < args.length; i++)
    str += (i != 0 ? "," : "") + "args[" + i + "]";
    eval("var result = obj.___fn(" + str + ");");
    obj.___fn = null;
    return result;
    };
    }

    The script seems to do the trick for IE ..

    Regards,
    Sebastian
    , Jul 19, 2006
    #5
  6. web.dev Guest

    Sevinfooter wrote:
    > you could initialize the argument variables outside of any functions,
    > which will make them global.
    >
    > var arguments = new Array;
    > function ....


    No, you can not do this. If you actually tried testing your code
    before posting, then you would've realized this. The arguments
    variable you've declared and the functionName.arguments object are in
    different scopes. Even if the OP had assigned the
    functionName.arguments object to the global variable, it would not do
    what you expected.

    For example:

    var arguments = "Should be overwritten";

    function foo()
    {
    arguments = foo.arguments;

    alert(typeof arguments);
    }

    foo("arg1", "arg2", "arg3");
    alert(typeof arguments);

    You'll notice the global variable type has not changed even after the
    supposed assignment.
    web.dev, Jul 19, 2006
    #6
  7. RobG Guest

    web.dev wrote:
    > Sevinfooter wrote:
    > > you could initialize the argument variables outside of any functions,
    > > which will make them global.
    > >
    > > var arguments = new Array;


    You can call the Array function as a constructor using:

    new Array();
    new Array;
    Array();
    Array;

    However the first version is the most popular. It is usually simpler
    to use an initialiser:

    var arguments = [];

    > > function ....


    Pitty the rest of the code wasn't supplied, it is left to the
    imagination. Anyhow, declaring the global variable 'arguments' as an
    Array suggests that within the function something like the following
    will be used:

    function foo(arg1, arg2){
    var i = arguments.length;
    do {
    window.arguments = arguments;
    } while (i--);

    // rest of function

    }

    It could also suggest the belief that a function's arguments object is
    an Array, which it isn't. It is much simpler to just assign a
    reference to the function's arguments object, in which case whatever
    the global arguments variable was initialised to is irrelevant - it may
    as well be declared without initialisation:

    var arguments;


    > No, you can not do this.


    Yes you can, just not exactly the way you've tried it. :)

    [...]
    > The arguments
    > variable you've declared and the functionName.arguments object are in
    > different scopes.


    Yes, but you forgot deal with that in your example.


    > Even if the OP had assigned the
    > functionName.arguments object to the global variable, it would not do
    > what you expected.


    Supposing the OP did do that, then you should be not be saying 'even
    if' but 'because'. If Sevinfooter had provided more code, we'd know
    whether your guess was right or wrong - giving Sevinfooter the benefit
    of doubt, you guessed wrong.


    > For example:
    >
    > var arguments = "Should be overwritten";
    >
    > function foo()
    > {
    > arguments = foo.arguments;


    When resolving variable names, JavaScript first looks in the local
    scope. When the code is called, arguments refers to the local arguments
    object, as does foo.arguments (though normally it is referenced as
    simply 'arguments') so they both reference the same local object and
    the global arguments property is not modified.

    The 'fix' is to either change the name of the global variable so it
    isn't masked by a local variable of the same name, or refer to it
    explicitly:

    window.arguments = arguments;

    [...]


    --
    Rob
    RobG, Jul 20, 2006
    #7
  8. RobG wrote:
    > web.dev wrote:

    <snip>
    >>> var arguments = new Array;

    >
    > You can call the Array function as a constructor using:
    >
    > new Array();
    > new Array;
    > Array();
    > Array;

    <snip>

    Not the last. Array - just evaluates to a reference to the array
    constructor function, and that evaluation has no side effects.

    Richard.
    Richard Cornford, Jul 20, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    643
    sushant
    Jan 20, 2005
  2. tutmann
    Replies:
    4
    Views:
    429
  3. jmborr
    Replies:
    1
    Views:
    399
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    805
  5. Navaneeth
    Replies:
    4
    Views:
    543
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page