SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    531
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pass function through another...

    wasn't sure how to write the subject, but here's what i want...

    Code:
    function myFunction(text, number, action) {
         // stuff
         action;
         // more stuff
    }
    
    function anotherFunction(message) {
         alert(message);
    }
    
    function yetAnotherFunction(message) {
         confirm(message);
    }
    
    myFunction("sometext", 9, anotherFunction());
    that way i can pass different functions through depending on what's needed. see what i mean?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the call method as show in the code below to do this
    function myFunction(text, number, action) {
    action.call(this, text, number);
    };

    function anotherFunction(message) {
    alert(message);
    };

    function yetAnotherFunction(message) {
    confirm(message);
    };

    myFunction("sometext", 9, yetAnotherFunction);

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    531
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome...that's the little thing i was missing. appreciated.

  4. #4
    SitePoint Enthusiast Moridin_8's Avatar
    Join Date
    Apr 2007
    Location
    UK, soon in USA
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking try this...

    Code:
    function myFunction(text, action) 
    {
         // stuff
         return action(text);
         // more stuff
    }
    
    function anotherFunction(message) 
    {
         alert(message);
    }
    
    function yetAnotherFunction(message) 
    {
         return confirm(message);
    }
    
    // The alert version
    myFunction("sometext1", anotherFunction);
    
    // The confirm version
    if(myFunction("sometext2", yetAnotherFunction))
    	alert("yo");
    else
    	alert("cya");
    
    // Anon function...
    myFunction("sometext3", function(thing){ alert(thing); });
    Matt "Moridin8" Warren
    ======================================
    Website: www.csharp-architect.com
    ....IRC: Usually freenode... '##csharp'


Bookmarks

Posting Permissions

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