Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11742 > unrolled thread
| Started by | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| First post | 2012-02-13 22:34 +0100 |
| Last post | 2012-02-14 23:02 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.javascript
Function named and called by a parameter value? Tuxedo <tuxedo@mailinator.com> - 2012-02-13 22:34 +0100
Re: Function named and called by a parameter value? Andreas Bergmaier <andber93@web.de> - 2012-02-13 23:32 +0100
Re: Function named and called by a parameter value? Stefan Weiss <krewecherl@gmail.com> - 2012-02-14 00:10 +0100
Re: Function named and called by a parameter value? Tuxedo <tuxedo@mailinator.com> - 2012-02-14 23:02 +0100
| From | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| Date | 2012-02-13 22:34 +0100 |
| Subject | Function named and called by a parameter value? |
| Message-ID | <jhbvlg$mb5$1@news.albasani.net> |
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
[toc] | [next] | [standalone]
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Date | 2012-02-13 23:32 +0100 |
| Message-ID | <jhc32o$sv9$1@news.albasani.net> |
| In reply to | #11742 |
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
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2012-02-14 00:10 +0100 |
| Message-ID | <gbWdnb0ZT_n3BaTSnZ2dnUVZ8uGdnZ2d@giganews.com> |
| In reply to | #11744 |
On 2012-02-13 23:32, Andreas Bergmaier wrote:
> Tuxedo schrieb:
> > 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?
Yes, I thought so, too. While it's possible to get something similar to
dynamically named variables and functions in JS, like you (and Martin
and RobG) described, in my experience it's rarely necessary or a good
idea. This type of question is common enough, especially from people
coming from a language where such constructs are supported natively
(like PHP or Perl), but it raises an automatic red flag in my mind.
There are special situations, like Adapter/Translator objects, where
function dispatch maps can come in handy. Some "magical" functions, like
autoloaders, can also be built like that, but in practice, these cases
are rare. I'm wondering why the OP wants to dynamically create named
variables and functions. I suspect that there may be a better way to
design what he's looking for.
It's commendable to reduce posted code examples to the required minimum,
but in this case I'd like to see the bigger picture :)
- stefan
[toc] | [prev] | [next] | [standalone]
| From | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| Date | 2012-02-14 23:02 +0100 |
| Message-ID | <jhelkr$quo$1@news.albasani.net> |
| In reply to | #11746 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web