Connecting Tech Pros Worldwide Forums | Help | Site Map

call function from within another

Andrew Poulos
Guest
 
Posts: n/a
#1: Nov 7 '08
Say I have this:

var foo = function(x, y) {
return x + y;
};

var bar = function(fname, param1, param2) {
/*
* how do I call the function "foo"
* and pass parameters to it?
*/
window[fname];
};

var tot = bar(foo, 1, 2);

I'm unsure about how to call "foo" from within "bar" and have parameters
passed to "foo".

Andrew Poulos

richard
Guest
 
Posts: n/a
#2: Nov 7 '08

re: call function from within another


On Fri, 07 Nov 2008 14:24:40 +1100, Andrew Poulos
<ap_prog@hotmail.comwrote:
Quote:
>Say I have this:
>
>var foo = function(x, y) {
return x + y;
>};
>
>var bar = function(fname, param1, param2) {
/*
* how do I call the function "foo"
* and pass parameters to it?
*/
window[fname];
>};
>
>var tot = bar(foo, 1, 2);
>
>I'm unsure about how to call "foo" from within "bar" and have parameters
>passed to "foo".
>
>Andrew Poulos

Might try function dothis(foo=x)
no quotes.

comp.lang.javascript
Andrew Poulos
Guest
 
Posts: n/a
#3: Nov 7 '08

re: call function from within another


richard wrote:
Quote:
On Fri, 07 Nov 2008 14:24:40 +1100, Andrew Poulos
<ap_prog@hotmail.comwrote:
>
Quote:
>Say I have this:
>>
>var foo = function(x, y) {
> return x + y;
>};
>>
>var bar = function(fname, param1, param2) {
> /*
> * how do I call the function "foo"
> * and pass parameters to it?
> */
> window[fname];
>};
>>
>var tot = bar(foo, 1, 2);
>>
>I'm unsure about how to call "foo" from within "bar" and have parameters
>passed to "foo".
>>
>Andrew Poulos
>
>
Might try function dothis(foo=x)
no quotes.
>
Alas your reply is too abbreviated for me to make sense of it.

Andrew Poulos
RobG
Guest
 
Posts: n/a
#4: Nov 7 '08

re: call function from within another




Andrew Poulos wrote:
Quote:
Say I have this:
>
var foo = function(x, y) {
return x + y;
};
>
var bar = function(fname, param1, param2) {
/*
* how do I call the function "foo"
* and pass parameters to it?
*/
window[fname];
That won't work: fname is a reference to a function object, it doesn't
know its "name" (there are many posts in clj by people wanting to know
a function's name, and many replies saying you can't reliably tell and
besides it's irrelevant).

Used inside a square bracket property accessor, fname it will be
resolved to a string by calling its toString method, so you are
effectively trying to call:

window[ foo.toString() ];

which is effectively:

window['function foo(x, y) {...}']

Quote:
};
>
var tot = bar(foo, 1, 2);
>
I'm unsure about how to call "foo" from within "bar" and have parameters
passed to "foo".
Not too difficult.

The original call is global code, it passes a reference to foo() to
bar(). That reference is assigned to bar’s local variable fname due
to the ordering of the parameter list. So now you just call it with
the other parameters as arguments:

return fname(param1, param2);


The return is there because I assume you want the result assigned to
tot.

--
Rob
Stevo
Guest
 
Posts: n/a
#5: Nov 7 '08

re: call function from within another


Andrew Poulos wrote:
Quote:
Say I have this:
>
var foo = function(x, y) {
return x + y;
};
>
var bar = function(fname, param1, param2) {
/*
* how do I call the function "foo"
* and pass parameters to it?
*/
window[fname];
};
>
var tot = bar(foo, 1, 2);
>
I'm unsure about how to call "foo" from within "bar" and have parameters
passed to "foo".
>
Andrew Poulos
fname(param1,param2);

Use fname exactly as you would use foo. Outside of the bar function,
you'd call foo(param1,param2). As you're passing foo into the bar
function, do the same thing except using the fname ....
fname(param1,param2);
slebetman
Guest
 
Posts: n/a
#6: Nov 7 '08

re: call function from within another


On Nov 7, 11:24*am, Andrew Poulos <ap_p...@hotmail.comwrote:
Quote:
Say I have this:
>
Almost right, try this:

/* create an anonymous function,
assign a reference to it to variable foo:
*/
var foo = function(x, y) {
* *return x + y;
};

var bar = function(fnref, param1, param2) {
/* this function accepts a function reference
(not function name, which is a string)
and executes it, passing two parameters:
*/
fnref(param1,param2);
};

/* call function referred to by bar passing
the function reference foo and integers
1 and 2:
*/
var tot = bar(foo, 1, 2);

/* I personally think it is always a mistake to
try to pass functions by "name string".
Javascript properly supports lambdas so passing
function references around is much more robust
and is easier to read.
*/
Michael Wojcik
Guest
 
Posts: n/a
#7: Nov 7 '08

re: call function from within another


slebetman wrote:
Quote:
Javascript properly supports lambdas
I don't want to be unnecessarily pedantic,[1] but "supports
first-class functions" would be preferable. ("Supports lambda" would
be OK, too, referring to the lambda operator. But that would likely be
confusing for some readers, since ECMAScript doesn't have a "lambda"
keyword, unlike LISP and friends.)

Lambda is the name of the abstraction operator. It creates a function.
In many languages it also creates a closure, which is the function
plus its bound variables; many people use "closure" to refer to the
function, which is not technically correct but close enough for many
purposes.

Calling a function or closure a "lambda" is like calling a sum an "add".

What you are saying above is that ECMAScript lets you pass function
references as parameters, and then apply them. That's a consequence of
having first-class functions.

Of course, there are languages where functions are not first-class,
but you can still pass function references and apply them. C, for
example, where functions cannot be created during program execution
(no dynamic functions) but can be called by reference. For example:

-----
int foo(int x, int y) { return x + y; }
int bar(int(*fnref)(int,int), p1, p2) { return fnref(p1, p2); }
int main(void) { return bar(foo, 1, 2); }
-----

(Note that in C a function pointer is implicitly dereferenced by the
function-call operator, so it's not necessary to write "(*fnref)" when
calling it.)

In COBOL, where functions ("programs") are again not first-class, you
can call a function literally using a string literal, or through a
reference, or using a string variable that contains its name:

call "foo" using 1 2
set fnref to entry "foo"
call fnref using 1 2
move "foo" to fname
call fname using 1 2

which makes call-through-reference and call-with-name-lookup almost
indistinguishable to the casual programmer.


[1] This is a lie, of course. I love pedantry as much as the next poster.

--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
Dr J R Stockton
Guest
 
Posts: n/a
#8: Nov 8 '08

re: call function from within another


In comp.lang.javascript message <gf28af01omv@news1.newsguy.com>, Fri, 7
Nov 2008 12:48:15, Michael Wojcik <mwojcik@newsguy.composted:
Quote:
>
>[1] This is a lie, of course. I love pedantry as much as the next poster.
But how can you tell who the next poster will be?

--
(c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@physics.org
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)
Michael Wojcik
Guest
 
Posts: n/a
#9: Nov 8 '08

re: call function from within another


Dr J R Stockton wrote:
Quote:
In comp.lang.javascript message <gf28af01omv@news1.newsguy.com>, Fri, 7
Nov 2008 12:48:15, Michael Wojcik <mwojcik@newsguy.composted:
Quote:
>[1] This is a lie, of course. I love pedantry as much as the next poster.
>
But how can you tell who the next poster will be?
I have my newsreader sort postings by increasing pedantry.

--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
John G Harris
Guest
 
Posts: n/a
#10: Nov 11 '08

re: call function from within another


On Fri, 7 Nov 2008 at 12:48:15, in comp.lang.javascript, Michael Wojcik
wrote:

<snip>
Quote:
>Of course, there are languages where functions are not first-class,
>but you can still pass function references and apply them. C, for
>example, where functions cannot be created during program execution
>(no dynamic functions) but can be called by reference. For example:
<snip>

On the other hand, C++ function objects are user-defined objects that
can inherit, can have a type template, can have state data, and can be
called. The objects can be copied and different instances can have
different states.

It's a lot cleaner than using a javascript closure to do some of this.

John
--
John Harris
Closed Thread