SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pass function name in arguments

    Say I wanted to call a function like so

    doSomething('test', "aFunction");

    how could I have the doSomething function later called aFunction, or whatever was passed?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    Say I wanted to call a function like so

    doSomething('test', "aFunction");

    how could I have the doSomething function later called aFunction, or whatever was passed?
    Presumably you mean that you want to convert a string into the name of a function?
    Code:
    function doSomething( param1, funcNameString )
    {
     ..........
    
     window[ funcNameString ]();
    
    }
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Presumably you mean that you want to convert a string into the name of a function?
    Code:
    function doSomething( param1, funcNameString )
    {
     ..........
    
     window[ funcNameString ]();
    
    }
    PERFECT! How did you know about this -- what is it called? I searched google for "variable function" but got nowhere. Is it supported in all browsers?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not aware of a term for it. It's just one of the basics of how variables and functions work in JavaScript. So, yes, it's supported by all browsers that support JavaScript.

    All variables and functions are properties and methods, respectively, of some object. Global variables and functions are properties and methods, respectively, of the global object, which is known as "window", among other things. Thusly you can access (get references for) global variables and functions via window.variableName (using dot syntax) or window['variableName'] (using array syntax).
    Last edited by Kravvitz; Apr 27, 2007 at 22:13.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    I'm not aware of a term for it. It's just one of the basics of how variables and functions work in JavaScript. So, yes, it's supported by all browsers that support JavaScript.

    All variables and functions are properties and methods, respectively, of some object. Global variables and functions are properties and methods, respectively, of the global object, which is known as "window", among other things. Thusly you can access (get references for) global variables and functions via window.variableName (using dot syntax) or window.['variableName'] (using array syntax).
    How come you use "dots" in your examples, but not in your original answer - which works...?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added one dot by mistake. I edited my post to fix it.

    They should look like this:
    Code:
    window.variableName
    window['variableName']
    I should add that when you use array syntax you can use a string variable in place of a string. Logic Ali demonstrated that in his answer.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    PERFECT! How did you know about this -- what is it called? I searched google for "variable function" but got nowhere. Is it supported in all browsers?
    In javascript function is a value like any other. You can assign functions to variables and pass them as arguments just like any other type.

    Code:
    stringVar = "cow";
    funcVar = function() { alert(blah) }
    
    otherFunc(stringVar);
    otherFunc(funcVar);

  8. #8
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function in javascript is an object reference itself. This reading will literally help your understanding. More practises are needed if you are not well experienced with javascript

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    javascript is a pure OO language, that is, everything is an object reference, not only function.


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
  •