Function named and called by a parameter value?

Discussion in 'Javascript' started by Tuxedo, Feb 13, 2012.

  1. Tuxedo

    Tuxedo Guest

    Is it possible to create a dynamically named function by a parameter and
    then run the created function within the block of the initial function by
    parameter reference, without literally typing the name of the
    parameter-named function within the initial function block? Something like:

    function main_function(param){

    function ??? (){ // create a function named by the parameter value
    d = document.title;
    alert(d);
    }

    return ???() // run sub_function by a parameter reference

    }

    main_function('sub_function') // name the function, e.g. 'sub_function'

    Surely the construct would not look close to the above...

    Thanks for any ideas!

    Tuxedo
     
    Tuxedo, Feb 13, 2012
    #1
    1. Advertisements

  2. Tuxedo schrieb:
    > Is it possible to create a dynamically named function by a parameter and
    > then run the created function within the block of the initial function by
    > parameter reference, without literally typing the name of the
    > parameter-named function within the initial function block? Something like:


    It is exactly the same as with your variables in your former thread. As
    you store objects in variables, and functions are objects, and you can
    store any values within objects:
    Use objects!

    They work as maps. Surely you already know accessing object propertys
    with the dot syntax:

    var o = {a:1:, b:3};
    o.a; // 1

    But you can also access them with bracket syntax:

    var propname = "b";
    o[propname]; // 3

    So you will use a object for your problem, where you can store any
    values and even functions:

    var myfn = function(params) { <body>; };

    is also a valid way to store functions. Of course this one has no "name"
    (you can't set names of functions), but you can easily call it with

    myfn("something");

    > function main_function(param){
    >
    > function ??? (){ // create a function named by the parameter value
    > d = document.title;
    > alert(d);
    > }
    >
    > return ???() // run sub_function by a parameter reference
    >
    > }
    >
    > main_function('sub_function') // name the function


    That makes no sense. Why should you want to "name" a internal function,
    which you never will see, and which is called inside the global
    function? Just use

    function main_function() { // no "param"
    return alert(document.title);
    }

    regards,
    Bergi
     
    Andreas Bergmaier, Feb 13, 2012
    #2
    1. Advertisements

  3. Tuxedo

    Tuxedo Guest

    Stefan Weiss wrote:

    [...]

    > It's commendable to reduce posted code examples to the required minimum,
    > but in this case I'd like to see the bigger picture :)


    I will try and post the bigger picture when I have a bit more workable code.

    Tuxedo
     
    Tuxedo, Feb 14, 2012
    #3
    1. Advertisements

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    859
    velthuijsen
    Apr 16, 2004
  2. Terry Reedy
    Replies:
    5
    Views:
    392
    Bruno Desthuilliers
    May 11, 2009
  3. Mister B
    Replies:
    8
    Views:
    988
    Nick Keighley
    Aug 26, 2010
  4. Abdul Mohsin
    Replies:
    1
    Views:
    480
  5. AzamSharp
    Replies:
    2
    Views:
    413
  6. Tuxedo
    Replies:
    7
    Views:
    1,007
    Tuxedo
    Feb 14, 2012
  7. Christof Warlich

    named parameter idiom and inheritance

    Christof Warlich, Jun 14, 2012, in forum: C++
    Replies:
    8
    Views:
    940
    Alain Ketterlin
    Jun 17, 2012
  8. Isaac Won
    Replies:
    0
    Views:
    421
    Isaac Won
    Feb 6, 2013
Loading...