473,396 Members | 1,921 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,396 software developers and data experts.

Working with functions

I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?

// EXAMPLE

doSomething(5, 10, functionA, functionB);

function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();
} else {
myFunctionB();
}
}

function functionA() {
// some code
}

function functionB() {
// some code
}
Oct 25 '05 #1
12 1318
I suspect there is a better approach, but without knowing what your
underlying intention is it's hard to say... but you should look at the
eval function which will do what you want.

Jim

Oct 25 '05 #2

dx27s wrote:
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?

// EXAMPLE

doSomething(5, 10, functionA, functionB);

function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();
} else {
myFunctionB();
}
}

function functionA() {
// some code
}

function functionB() {
// some code
}


:) did you know you already got the solution? That is the correct
syntax for passing a function reference to another function.

Oct 25 '05 #3
dx27s said the following on 10/25/2005 3:52 PM:
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?

// EXAMPLE

doSomething(5, 10, functionA, functionB);

function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();


window[myFunctionA]();

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 26 '05 #4
JRS: In article <11**********************@g43g2000cwa.googlegroups .com>
, dated Tue, 25 Oct 2005 13:50:50, seen in news:comp.lang.javascript,
jd*******@gmail.com posted :
I suspect there is a better approach, but without knowing what your
underlying intention is it's hard to say... but you should look at the
eval function which will do what you want.

Jim


Obviously not Our Jim.

A good way to avoid (to some extent) writing complete twaddle is to
first read and understand the newsgroup FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 26 '05 #5
What's wrong with suggesting eval?

He can acheive the same *result* with eval, can he not?

doSomething(5, 10, "functionA", "functionB");
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
eval(myFunctionA+"()");
} else {
eval(myFunctionB+"()") }

}
function functionA() {
// some code
}
function functionB() {
// some code
My understanding was that this group was for *helping*, I didnt realise
we were comissioned to provide 100% accurate and knowledgable answers.
My advice, to see the eval function would have lead him to a reasonable
solution.

"Twaddle", POPPYCOCK!!

Jim

Oct 26 '05 #6
jd*******@gmail.com said the following on 10/26/2005 2:12 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
What's wrong with suggesting eval?
You mean other than the fact it isn't needed, makes the code slower
(albeit minimally) and there is a better alternative?
He can acheive the same *result* with eval, can he not?
No. But to understand that answer you have to understand eval itself.
doSomething(5, 10, "functionA", "functionB");
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
eval(myFunctionA+"()");
} else {
eval(myFunctionB+"()") }
}
function functionA() {
// some code
}
function functionB() {
// some code
My understanding was that this group was for *helping*, I didnt realise
we were comissioned to provide 100% accurate and knowledgable answers.
This newsgroup is for the "discussion of javascript" and has been
extended to cover JScript and ECMAScript. It is not a "help desk". If
you get an answer to your question - great. If not, you keep going. This
is NOT a help desk.
My advice, to see the eval function would have lead him to a reasonable
solution.


No, it would have lead him to using something that was not optimal nor
designed for that purpose. Did you see/try/test my solution? It doesn't
use eval.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 26 '05 #7

Randy Webb wrote:
jd*******@gmail.com said the following on 10/26/2005 2:12 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Sorry Randy, if I had done that it would have been obvious that I was
replying to the honourable Doctor, who shot down my suggestion and told
me to read the FAQ.

Your answer was more ideal, and I did note in my reply (which was
first) that there probably were better ways to do it.

Just trying to help out, but getting shot down is really annoying -
your suggestion was spot on, and I had no intention of arguing with you
- I really just wanted to know what issue the Doctor had with my
answer.

Sorry and thanks

Jim

Oct 26 '05 #8
jd*******@gmail.com said the following on 10/26/2005 2:25 PM:
Randy Webb wrote:
jd*******@gmail.com said the following on 10/26/2005 2:12 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Sorry Randy, if I had done that it would have been obvious that I was
replying to the honourable Doctor, who shot down my suggestion and told
me to read the FAQ.


I am not sure my opinion of him is the same as yours.
Just trying to help out, but getting shot down is really annoying -
your suggestion was spot on, and I had no intention of arguing with you
- I really just wanted to know what issue the Doctor had with my
answer.


That's typical of John. After a while you learn to cope with him. As for
getting shot down, if I had a US Dollar for every time I have been shot
down in this group I would retire to the Bahamas somewhere. Don't let it
discourage you, take it as a learning process.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 26 '05 #9
dx27s <dx***@yahoo.com> writes:
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is
there perhaps a better approach entirely?


You got it. That's exactly how I would do it too.

In Javascript, functions are first class values, and can be
passed as arguments to other functions (or themselves, even :)

There is no need to pass around the names of functions, it just
means that you have to do an extra lookup later. And don't
use "eval" for resolving names, even if you do pass them around.
It's too fragile compared to other ways of doing name resolution.

You can also return a function, so one different approach would
let you return a choice of a function:

---
function selectFunc(x, y, funcA, funcB) {
if (x > y) {
return funcA;
} else {
return funcB;
}
}

function functionA(x) {
document.write(y);
}
function functionB(y) {
alert(x);
}

doSomething(5, 10, functionA, functionB)("Hello world");

---

Or even inline:
---
var x = 5, y=10;
((x > y) ? functionA : functionB)("Hello world");
---

On the other hand, if what you have solves your problem, there is
absolutely nothing wrong with it, and there isn't anything that is
consistently better.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 27 '05 #10
Randy Webb <Hi************@aol.com> writes:
doSomething(5, 10, functionA, functionB);
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();


window[myFunctionA]();


that would require changing the call to:
doSomething(5, 10, "functionA", "functionB");
i.e., passing the name instead of the function itself ... which there
isn't any good reason to do.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 27 '05 #11
Lasse Reichstein Nielsen wrote:
dx27s <dx***@yahoo.com> writes:

I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is
there perhaps a better approach entirely?

You got it. That's exactly how I would do it too.


Thanks to everyone for all the help and insight.
Oct 27 '05 #12
JRS: In article <11**********************@g49g2000cwa.googlegroups .com>
, dated Wed, 26 Oct 2005 11:25:55, seen in news:comp.lang.javascript,
jd*******@gmail.com posted :

Randy Webb wrote:
jd*******@gmail.com said the following on 10/26/2005 2:12 PM:
Just trying to help out, but getting shot down is really annoying -
your suggestion was spot on, and I had no intention of arguing with you
- I really just wanted to know what issue the Doctor had with my
answer.


Mainly its pig-headedness.

--
© John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Oct 27 '05 #13

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

Similar topics

2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
3
by: Torch | last post by:
I have some javascript that has been working for a year - I have just upgraded to IE6 and now its stopped working. The page consists of a parent window containing some functions and an iframe...
14
by: Paul | last post by:
Hi I have 2 functions in java script, one opens a second window-this works, the other is supposed to close this second window, does not seem to be working. Just wondering if anyone had any ideas....
0
by: Sarah J | last post by:
Hi We have a high traffic PHP built website which, for reasons beyond my control, is hosted on a twin processor (Zeus) Windows 2003/IIS6 server. PHP 4.4.1 came preinstalled as a CGI but because...
3
by: Jason Huang | last post by:
Hi, In our C# Windows Form application, we are using the SQL Server 2000 as the database server. The Database table MyTable has a field RegistrationDate which represents the Date a client comes...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
3
by: bbcrock | last post by:
I take my hat in hand with a question. I have dynamically named form elements set in divs based on a server-side language. I am displaying these using DHTML in a display:none, display:block...
4
by: David | last post by:
Hi, I have taken some SQL from MS Access and got it working. I have since updated the SQL in Access but cannot get the additional part working in ASP. The working SQL I have in asp is:- ...
3
by: Alexey Kulentsov | last post by:
Hi All! Is here any solution? APC and shm_ functions doesn't work from command line but I need it.
22
by: MLH | last post by:
100 Dim db As Database, rst As Recordset 120 Set db = CurrentDb 140 PString = "SELECT qryBatchList.ReadyFor906, qryBatchList.BatchID FROM qryBatchList WHERE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.