Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to Set Function Name as Argument of Another Function?

    Hi,
    I want to pass a function A name as an argument to function B because I need to use different functions to do something in function B. At the same time, I am using the arguments[] in function B because I am don't know how many arguments will be in the function B. I also need the arguments[0]() to be able to accept any # of arguments.
    e.g.
    Code:
    function B() {
      var data;
      arguments[0](data);
    }
    
    B(A);
    It doesn't seem to work. I need it to work in both IE and firefox.

    Many thanks.
    Last edited by dealmaker; 12-14-2005 at 08:04 AM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by dealmaker
    Code:
      arguments[0](data);
    You're missing an 's' there.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Actually, I have a 's' in the actual code, it doesn't work. I just mistyped in this thread, and I corrected it.

    Quote Originally Posted by liorean
    You're missing an 's' there.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, this works:
    Code:
    function B(){
        var
            data='b>data',
            i=0,
            l=arguments.length;
        while(i<l)
            arguments[i++](data);
    }
    
    function A(s){
        alert(s);
    }
    
    B(A);
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Actually, only one of the arguments of B is function, the rest are just variables. And what is 'b>data'? Is it a typo? And I need arguments(data) to accept any # of variables, not just one variable.

    Quote Originally Posted by liorean
    Well, this works:
    Code:
    function B(){
        var
            data='b>data',
            i=0,
            l=arguments.length;
        while(i<l)
            arguments[i++](data);
    }
    
    function A(s){
        alert(s);
    }
    
    B(A);

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by dealmaker
    Actually, only one of the arguments of B is function, the rest are just variables. And what is 'b>data'? Is it a typo? And I need arguments(data) to accept any # of variables, not just one variable.
    You declared the data variable yourself in your example. I just gave it a value to show you that it does, in fact, work.

    Code:
    function B(fn){
        var
            a=[],
            i=1,
            l=arguments.length;
        while(i<l)
            a.push(arguments[i++]);
        return fn?
            fn.apply(this,a):
            undefined;
    }
    
    function A(){
        var
            a=[],
            i=0,
            l=arguments.length;
        while(i<l)
            a.push(arguments[i++]);
        return('Arguments to A:\n----\n'+a.join(',\n'));
    }
    
    alert(B(A,0,1,2,3,4));
    alert(B(A));
    alert(B());
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •