473,700 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Aug 27 '08 #1
6 1141
sheldonlg wrote:
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/
Aug 27 '08 #2
Martin Honnen wrote:
sheldonlg wrote:
>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?
Aug 27 '08 #3
sheldonlg wrote:
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/
Aug 27 '08 #4
In comp.lang.javas cript message <48************ ***********@new sspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<ma*******@yaho o.deposted:
>sheldonlg wrote:
>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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 27 '08 #5
Dr J R Stockton wrote:
In comp.lang.javas cript message <48************ ***********@new sspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<ma*******@yaho o.deposted:
>sheldonlg wrote:
>>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="getVal ues(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.
Aug 28 '08 #6
Dr J R Stockton wrote on 27 aug 2008 in comp.lang.javas cript:
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(){retu rn 'It's raining again'};
};

alert (Th());

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1386
by: alltel news | last post by:
Off topic, I know... In a recent Google search for the "gods of programming" I saw several references to there actually being twelve of them. Does anybody know who they are? I'd guess that Knuth, Kernigan, and Ritchie are three of the twelve. Thanks for humoring me, Ken Webster
3
331
by: Angus Comber | last post by:
Hello I am attempting to build a set of classes which handle telephony. Basically the handling of phone calls. My design currently has two main classes - one dealing with the device and the other with actual calls. So you could say I have a device class and a calls class. The device here is a telephone handset basically. Telephones connected to business switchboards can hanlde more than one call at a time - eg you can have one...
11
2552
by: enki | last post by:
I am writing a game and I am having trouble with moving the character on the map. Here is what I have right now. It involves win32 programming but that not my problem. I would like some suggestions how I can make my code better. If you need more code I will post it. I am in early devlopment of the game. map.h:
3
2035
by: Joe | last post by:
Hi, I am new to creating .asp web sites and have a basic question. I have a website with .html pages running on an IIS server with .Net installed. I also have a couple of forms on the website. Some of them send email and others store information entered by the user in the MS Access DB. The database is small at this time and is growing slowly. The forms are .aspx pages coded using VB.NET. These .aspx pages work fine. Although the...
10
2141
by: Avi | last post by:
Hi I need to read in a large set of text files (9GB+ each) into a database table based on fixed width lengths. There are several ways to complete this, but I am wondering if anyone has insight into the FASTEST way to read in the data and chop it up ahead of posting it into the DB. So far, things work, but they are far slower than expected.
4
257
by: CSharpguy | last post by:
I'm not sure if this is the correct forum or not, but I have a basic question. Currently we have are doing calculations via stored procedures and then returning the results back to the client in either a web page or a winForm style application. There are times that the stored procedure takes 20 minutes or 3 hours to run. Now, our database person says that all the calculations needs to be removed from SQL and put in either the web...
14
1772
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays, but I purposely had this array filled unsequentially because the key for the array is meant to act as an ID which has a contextual meaning in my script. The problem, of course, is that for...in also returns my method 'contains' as one of the...
40
1672
by: RvGrah | last post by:
I've been writing in C# for about 4 years now, coming from VB.net and VB6 before that, in which I know I'm not alone. I found learning C#, at least to the extent that I use it in developing database front- ends, to be rather painless. The language and VS ide seemed comfortable pretty quickly. Some of the enhancements that have come along in the last two updates (via VS 2005 and 2008) like Generics and now Linq and anonymous types etc,...
3
157
by: H. S. Lahman | last post by:
Responding to siddharthkhare... Ignore Topmind and frebe. They are anti-OO P/R guys. Let's not confuse things with specific 3GL syntax. At the OOA/D level the model looks like: | 1
3
1943
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee objects that I can iterate through relatively easily. I've included code at the bottom of this message. I can pretty easily iterate through my employee objects like so: Dim theEmployees As Employees = New Employees
0
9209
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9069
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8964
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8918
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4653
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.