473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1129
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.javascript message <48***********************@newsspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<ma*******@yahoo.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.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.
Aug 27 '08 #5
Dr J R Stockton wrote:
In comp.lang.javascript message <48***********************@newsspool2.ar
cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen
<ma*******@yahoo.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="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.
Aug 28 '08 #6
Dr J R Stockton wrote on 27 aug 2008 in comp.lang.javascript:
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)
Aug 28 '08 #7

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

Similar topics

3
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...
3
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...
11
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...
3
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....
10
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...
4
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...
14
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,...
40
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...
3
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.