By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Dynamically generated function names

P: n/a
Hi,
Does anyone know how to make this work?

var sectionId = 5;
repeat_section_sectionId();

function repeat_section_5(){
alert("firing");
}

The function's name contains the variables value, so how do we make
the engine see that?

Thanks in advance for any help.
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a


Falc2199 wrote:

Does anyone know how to make this work?

var sectionId = 5;
repeat_section_sectionId();

function repeat_section_5(){
alert("firing");
}

The function's name contains the variables value, so how do we make
the engine see that?


You can use
window['repeat_section_' + sectionId]();

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
On 22 Nov 2004 07:33:00 -0800, Falc2199 <Je*******@aol.com> wrote:

[snip]
var sectionId = 5;
repeat_section_sectionId();

function repeat_section_5(){
alert("firing");
}

The function's name contains the variables value, so how do we make the
engine see that?


Using square bracket notation to construct the name:

window['repeat_section_' + sectionId]();

See <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html> for
more information.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

P: n/a
Falc2199 wrote:
Hi,
Does anyone know how to make this work?

var sectionId = 5;
repeat_section_sectionId(); Try:
window['repeat_section_'+sectionId]();
function repeat_section_5(){
alert("firing");
}


A better solution though is to put your functions in an array and then you
can just subscript the array directly:

var repeat_section = new Array();
repeat_section[5] = function() { alert("firing"); }

if (repeat_section[sectionId]) {
repeat_section[sectionId]();
}
Jul 23 '05 #4

P: n/a
Duncan Booth wrote:
Falc2199 wrote:

Hi,
Does anyone know how to make this work?

var sectionId = 5;
repeat_section_sectionId();


Try:
window['repeat_section_'+sectionId]();
function repeat_section_5(){
alert("firing");
}

A better solution though is to put your functions in an array and then you
can just subscript the array directly:

var repeat_section = new Array();
repeat_section[5] = function() { alert("firing"); }

if (repeat_section[sectionId]) {
repeat_section[sectionId]();
}


And how is creating an unnecessary array a "better solution" than simply
accessing the properties/functions of the window object?

if (window['repeat_section_' + sectionID]())
{window['repeat_section_'+sectionId]();}

does the same exact thing yours does without the added overhead of an array.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

P: n/a
On Mon, 22 Nov 2004 12:40:50 -0500, Randy Webb <Hi************@aol.com>
wrote:

[snip]
if (window['repeat_section_' + sectionID]())
{window['repeat_section_'+sectionId]();}

does the same exact thing yours does without the added overhead of an
array.


Well, almost. You called the function, rather than just evaluated the
reference. :)

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

P: n/a
Randy Webb wrote:
And how is creating an unnecessary array a "better solution" than
simply accessing the properties/functions of the window object?

if (window['repeat_section_' + sectionID]())
{window['repeat_section_'+sectionId]();}

does the same exact thing yours does without the added overhead of an
array.


Which differs from the first of my two proposed solutions (the one you
snipped) how exactly? (Apart from mine not having the test, or for that
matter the typo)

As to why creating an array is a "better solution", the window object is
global to the javascript running in that window. If you have a lot of
javascript then minimising the number of global variables is a good thing
for the same reason as it is in any other programming language: it
generally makes your life easier by reducing the time you have to spend
thinking about possible variable name collisions and reducing the bugs that
come when you get it wrong.

It also has other benefits. If you want to find out which 'repeat_section'
functions exist, it may be simpler to examine an array rather than trying
to pick them out of all the window properties. Also if you actually want to
access the functions from several different places you don't have to keep
retyping that string addition (another possible source of bugs if you
mistype the string in the wrong place).

How many reasons did you want? Is someone charging you for the arrays you
create?
Jul 23 '05 #7

P: n/a
Duncan Booth wrote:
Randy Webb wrote:

And how is creating an unnecessary array a "better solution" than
simply accessing the properties/functions of the window object?

if (window['repeat_section_' + sectionID]())
{window['repeat_section_'+sectionId]();}

does the same exact thing yours does without the added overhead of an
array.

Which differs from the first of my two proposed solutions (the one you
snipped) how exactly? (Apart from mine not having the test, or for that
matter the typo)


None, but you offered the array approach as a "better solution". But as
was pointed out, the if statement in mine should be:

if (window['repeat_section_' + sectionID])

And yes, I had a typo.
As to why creating an array is a "better solution", the window object is
global to the javascript running in that window. If you have a lot of
javascript then minimising the number of global variables is a good thing
for the same reason as it is in any other programming language: it
generally makes your life easier by reducing the time you have to spend
thinking about possible variable name collisions and reducing the bugs that
come when you get it wrong.
Unless you use inner functions, functions are global as well. As are
arrays. But functions are a property of the window object(except inner
functions).

With an array:

Check to see if the array entry exists.
Call the function.
That calls the global object and asks for the function.
The global object checks for the function.
Then the function is run.

Without the array, you skip a step of having to look it up in the array.
You also skip the overhead of the array.
You also minimize the chance of an error happening when you create the
array, unless you create it dynamically from all the function names in
the document.
It also has other benefits. If you want to find out which 'repeat_section'
functions exist, it may be simpler to examine an array rather than trying
to pick them out of all the window properties.
The window object does it for you. In fact, when you search an array, it
searches the window properties/objects to find that array and then
searches it.
Also if you actually want to access the functions from several different
places you don't have to keep retyping that string addition (another possible
source of bugs if you mistype the string in the wrong place).
I don't see where that has any benefits. There is just as much chance of
typing a type when typing the array call as there is with a function
call. Either way, you have to type it out.
How many reasons did you want?
Some that actually made sense to me.
Is someone charging you for the arrays you create?


In overhead and memory, yes.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8

P: n/a
Randy Webb wrote:
Duncan Booth wrote:
As to why creating an array is a "better solution", the window object
is global to the javascript running in that window. If you have a lot
of javascript then minimising the number of global variables is a
good thing for the same reason as it is in any other programming
language: it generally makes your life easier by reducing the time
you have to spend thinking about possible variable name collisions
and reducing the bugs that come when you get it wrong.
Unless you use inner functions, functions are global as well. As are
arrays. But functions are a property of the window object(except inner
functions).


I think you have a fundamental misunderstanding here. A function is global
if it is assigned to a global variable (or defined as a named function, but
that is just another way to assign it to a global variable). It doesn't
matter whether it is an inner function or not, its the variables it is
assigned to that matter.

With an array:

Check to see if the array entry exists.
Call the function.
That calls the global object and asks for the function.
The global object checks for the function.
Then the function is run.
No, that is plain wrong. Even if the function is defined as global this two
step lookup you describe doesn't happen. The sequence is actually:

Check to see if the array entry exists.
Call the function.
Then the function is run.

No global object lookups are involved unless the array itself is global.
Remember that a function is simply an object, once it has been assigned
into the array you can reassign or delete the original global name (if
there even was one).
Without the array, you skip a step of having to look it up in the
array. You also skip the overhead of the array.
Without the array the sequence is:

Evaluate a string expression
Look up the function in the window object
Check to see if the function exists
Call the function
then the function is run

Both of these, as originally written involve extra steps, since we both
did the lookup of the function twice (once for the check and once for the
call), but you can easily store the intermediate value in a variable to
avoid that.
You also minimize the chance of an error happening when you create the
array, unless you create it dynamically from all the function names in
the document.
I don't understand this comment. I don't see how creating the array is
either more or less error prone than declaring the function normally.

function repeat_section_5() { ... }

versus

repeat_section[5] = function() { ... }
It also has other benefits. If you want to find out which
'repeat_section' functions exist, it may be simpler to examine an
array rather than trying to pick them out of all the window
properties.
The window object does it for you. In fact, when you search an array,
it searches the window properties/objects to find that array and then
searches it.


If the array is stored in the window object then, yes, accessing the array
will involve a lookup on the window object. Once you have the array (one
global lookup) you can then pass the collection of functions around as a
parameter, or iterate through it calling several of them, or manipulate it
in whatever other way seems appropriate without doing any further global
lookups. Looking up global variables (i.e. properties of window) in
javascript is MUCH slower than accessing local variables.
Also if you actually want to access the functions from several
different places you don't have to keep retyping that string addition
(another possible source of bugs if you mistype the string in the
wrong place).
I don't see where that has any benefits. There is just as much chance
of typing a type when typing the array call as there is with a
function call. Either way, you have to type it out.


I think (although you may disagree) that if you mistype the version based
on the string expression all that is likely to happen is that the function
never gets called. If you mistype the name of the array variable then you
get a javascript error.

e.g. compare
var method = window['repaet_section_'+sectionId]);
with
var method = repaet_section[sectionId];

The first simply sets method to undefined. The second throws an error.
How many reasons did you want?
Some that actually made sense to me.


I'm sorry if my explanations haven't done that for you.
Is someone charging you for the arrays you create?


In overhead and memory, yes.

I think that using arrays here is likely to end up with faster code. I do
know that I was able to considerably speed up some code I was writing
simply by reducing the number of global variable references (e.g. by
creating a local variable reference to a global function that was being
called many times).

As for memory use I would be very, very, very suprised if you could come up
with a concrete example where putting functions in an array instead of
declaring them separately actually used a measurably greater amount of
memory.

Also my version saves on all those function names. If you are really worred
about memory then 100 functions named 'repeat_section_1' to
'repeat_section_100' means you have 100 pointless strings lying around in
memory, whereas 100 anonymous functions in an array avoids that.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.