Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to add parameters to a function passed as a paramter?

Reply
Thread Tools

How to add parameters to a function passed as a paramter?

 
 
Steve Neill
Guest
Posts: n/a
 
      10-26-2004
Here's an interesting problem...

I have 2 functions, aFunc() and bFunc(). I call aFunc() passing a call
to bFunc() as a parameter. Function aFunc() executes bFunc(). Function
bFunc() lists the parameter values passed to it.

OK, here's the twist...

I want to add some parameters to bFunc() from inside aFunc(). Can that
be done?

Sorry no prizes, except the satisfaction of the solution

Regards,
Steve


aFunc(function() { bFunc(); } );

function aFunc(f) {
var x = 1;
var y = 2;

// add x, y as parameters to function f()
// ???

// execute the parameter function
f();
}

function bFunc() {
for (var i = 0; i < arguments.length; ++i) {
alert(arguments[i]);
}
}
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      10-27-2004
On 26 Oct 2004 14:21:29 -0700, Steve Neill <(E-Mail Removed)> wrote:

> I have 2 functions, aFunc() and bFunc(). I call aFunc() passing a call
> to bFunc() as a parameter. Function aFunc() executes bFunc(). Function
> bFunc() lists the parameter values passed to it.
>
> OK, here's the twist...
>
> I want to add some parameters to bFunc() from inside aFunc(). Can that
> be done?


Yes, but your call to aFunc needs to be changed.

Functions are objects, and just like all other objects, you can refer to
them with any number of identifiers. Consider:

function myFunction() {
}

var yourFunction = myFunction;

You can now invoke myFunction with either myFunction() or yourFunction()
with exactly the same results. For your particular case, this would become:

function aFunc(f) {
var x = 1,
y = 2;

f(x, y); // Call bFunc with arguments x and y.
}

function bFunc() {
for(var i = 0, n = arguments.length; i < n; ++i) {
alert(arguments[i]);
}
}

aFunc(bFunc); // Note: no parentheses on bFunc!

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
 
Bob Gregory
Guest
Posts: n/a
 
      10-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Steve Neill) wrote in message news:<(E-Mail Removed). com>...
> Here's an interesting problem...
>

<snip />

Am I missing something?

aFunc(bFunc);

function aFunc(f) {
var x = 1;
var y = 2;

// add x, y as parameters to function f()
// ???

// execute the parameter function
f(x,y);
}

function bFunc() {
for (var i = 0; i < bFunc.arguments.length; ++i) {
alert(bFunc.arguments[i]);
}
}
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-27-2004
On 27 Oct 2004 06:12:14 -0700, Bob Gregory <(E-Mail Removed)> wrote:

[snip]

> Am I missing something?


Not especially, but...

[snip]

> function bFunc() {
> for (var i = 0; i < bFunc.arguments.length; ++i) {
> alert(bFunc.arguments[i]);
> }
> }


....the arguments object is not a property of the function, but of the
function's activation object. Effectively, it is a local variable to
accessed like all other local variables.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Steve Neill
Guest
Posts: n/a
 
      10-27-2004
Thank you Michael. I didn't realize I could pass a function that way
(now I do!) This solves the problem -- Thank you
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Array passed as function parameters Why Tea C Programming 21 08-26-2012 10:22 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Add properties to function while in function with out specifying function ? Richard A. DeVenezia Javascript 4 09-05-2003 01:43 PM



Advertisments