Connecting Tech Pros Worldwide Help | Site Map

Question: probably basic one

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 27th, 2008, 11:15 AM
sheldonlg
Guest
 
Posts: n/a
Default Question: probably basic one

Simple overview:
I want to call a function in a javascript duntion where the name of the
called function is an argument value passed into the javascript function.

Here is what I want:

On the html pages:
page1: onclick="A('a', 'C')"

In the library:
function A(x, B) { y=...do stuff...; B(y);}

function C(x) {...}


Currently, I have A call a function D which has a switch statement to
call the proper C function. How can I have A call C directly using the
passed in name of C?

  #2  
Old August 27th, 2008, 11:35 AM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Question: probably basic one

sheldonlg wrote:
Quote:
Simple overview:
I want to call a function in a javascript duntion where the name of the
called function is an argument value passed into the javascript function.
>
Here is what I want:
>
On the html pages:
page1: onclick="A('a', 'C')"
>
In the library:
function A(x, B) { y=...do stuff...; B(y);}
>
function C(x) {...}
>
>
Currently, I have A call a function D which has a switch statement to
call the proper C function. How can I have A call C directly using the
passed in name of C?
Functions are first class objects so don't pass a function name in, pass
the function itself in e.g.
<div onclick="A('a', C);">
then you can simply use
function A(x, F) { y = ...; F(y); }

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #3  
Old August 27th, 2008, 11:55 AM
sheldonlg
Guest
 
Posts: n/a
Default Re: Question: probably basic one

Martin Honnen wrote:
Quote:
sheldonlg wrote:
Quote:
>Simple overview:
>I want to call a function in a javascript duntion where the name of
>the called function is an argument value passed into the javascript
>function.
>>
>Here is what I want:
>>
>On the html pages:
>page1: onclick="A('a', 'C')"
>>
>In the library:
>function A(x, B) { y=...do stuff...; B(y);}
>>
>function C(x) {...}
>>
>>
>Currently, I have A call a function D which has a switch statement to
>call the proper C function. How can I have A call C directly using
>the passed in name of C?
>
Functions are first class objects so don't pass a function name in, pass
the function itself in e.g.
<div onclick="A('a', C);">
then you can simply use
function A(x, F) { y = ...; F(y); }
>
You mean simply leave off the quotes in the initial call is all I have
to do?
  #4  
Old August 27th, 2008, 03:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Question: probably basic one

sheldonlg wrote:
Quote:
You mean simply leave off the quotes in the initial call is all I have
to do?
Yes, you can do that to pass the function as an argument to the other
function.


--

Martin Honnen
http://JavaScript.FAQTs.com/
  #5  
Old August 27th, 2008, 10:55 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: Question: probably basic one

In comp.lang.javascript message <48b572c9$0$12953$9b4e6d93@newsspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<mahotrash@yahoo.deposted:
Quote:
>sheldonlg wrote:
>
Quote:
>You mean simply leave off the quotes in the initial call is all I
>>have to do?
>
>Yes, you can do that to pass the function as an argument to the other
>function.
For those who really do want to pass in the name of the function as a
string (as originally asked), the notation shown by

window["LZ"](5)

is available; for me, that returns '05'. No doubt someone will comment
if that method is not always available.

If the function is frequently needed, consider :

Fn = window["LZ"]
Fn(5) + Fn(6) // gives '0506'

If, having received a function itself, one wants the name. one can
generally get it by a RegExp match on Fn.toString() ; js-nclds.htm .

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
  #6  
Old August 28th, 2008, 12:35 AM
sheldonlg
Guest
 
Posts: n/a
Default Re: Question: probably basic one

Dr J R Stockton wrote:
Quote:
In comp.lang.javascript message <48b572c9$0$12953$9b4e6d93@newsspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<mahotrash@yahoo.deposted:
Quote:
>sheldonlg wrote:
>>
Quote:
>>You mean simply leave off the quotes in the initial call is all I
>>have to do?
>Yes, you can do that to pass the function as an argument to the other
>function.
>
For those who really do want to pass in the name of the function as a
string (as originally asked), the notation shown by
>
window["LZ"](5)
>
is available; for me, that returns '05'. No doubt someone will comment
if that method is not always available.
>
If the function is frequently needed, consider :
>
Fn = window["LZ"]
Fn(5) + Fn(6) // gives '0506'
>
If, having received a function itself, one wants the name. one can
generally get it by a RegExp match on Fn.toString() ; js-nclds.htm .
>
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
>
I only asked about the name because that is what I thought I had to do.
However, what I want is more like;

onclick="getValues(p1, p2, postGetValues)";

function getValues(x, y, z) {
....gets w...
otherFunction(w, z);
}

funtion otherFunction(a, z) {
....gets b....
z(b);
}

postGetValues(x) {
...process x which gets its value from otherFunction...
}

As I understood mahotrash, this is what he meant and this is what I
wanted. The idea being that all I need write is the initially invoked
function (getValues) and the post-processing function (postGetValues),
eliminating the need for altering a third middle function which has a
switch statement to navigate properly.
  #7  
Old August 28th, 2008, 08:15 AM
Evertjan.
Guest
 
Posts: n/a
Default Re: Question: probably basic one

Dr J R Stockton wrote on 27 aug 2008 in comp.lang.javascript:
Quote:
For those who really do want to pass in the name of the function as a
string (as originally asked), the notation shown by
>
window["LZ"](5)
>
.... gives interesting be it not very useful possibilities
for batchwize assigning of functions:

<script type='text/javascript'>

var weekDay = 'x/Su/Mo/Tu/We/Th/Fr/Sa'.split('/');

for(var i=1;i<8;i++) {
window[weekDay[i]] = function(){return 'It's raining again'};
};

alert (Th());

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.