call function from within another

A

Andrew Poulos

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
 
R

richard

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
 
A

Andrew Poulos

richard said:
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
 
R

RobG

Andrew said:
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) {...}']

};

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.
 
S

Stevo

Andrew said:
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);
 
S

slebetman

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.
*/
 
M

Michael Wojcik

slebetman said:
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.
 
D

Dr J R Stockton

In comp.lang.javascript message <6d36dbc6-444b-41a7-9062-f3ec4e7a0e02@i1
8g2000prf.googlegroups.com>, Thu, 6 Nov 2008 20:51:25, RobG
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).

In practice, that "won't" combined with "reliably" need not be true.
And it can certainly be relevant.

Consider writing a function TEST whose first argument is a function, Fn,
and which tests Fn. It will be called as TEST (F1 ), TEST(F2 ), ...
TEST(Fz ) in order to test rival functions F1 to Fz all of which are
supposed to do the same thing. All of F1 to Fz are defined in the
obvious fashion and there is no question of assigning functions to
variables.

It may well be desirable for the test code to output the name of the
function currently being tested, and it can get that by
Fn.toString().somethingUsingASuitable(RegExp)

There is of course an alternative; to pass the names of the functions to
be tested as strings, and to use window[string] to access them within
TEST.


Writing a library function which in all circumstances can find an
appropriate name, if any, for a function reference given to it is
another matter.

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

John G Harris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top