Pre-defining a function that can pass variables.

This is a discussion on "Pre-defining a function that can pass variables." within the JavaScript Forum section. This forum, and the thread "Pre-defining a function that can pass variables. are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 22nd, 2006, 06:51
New Member
Join Date: Jul 2006
Location: Minnesota
Age: 21
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Pre-defining a function that can pass variables.

Now I'm not talking about:

function(var){
alert(var);
}

or like that.


I'm talking about create a function like:
var testFunction = Function('callMe();');

That's fine as well, but:

var testFunction = Functn('callMe(varToPass);');

will only pass the variable that is currently initialized. This make send to me, but what I'm looking to do is make a function (in a similar fashion I hope) that can pass a variable by the specified at a future time.

Say I'm setting up an object that I'm going to send to a function.

So,

var testObj;
testObj.callThisFunctionWhenDone = Function('callWhenDone(passThisVar)');
testObj.init();
testObj.start();

Now when the whole process is done, it is going to return some data and call the function created right here at the beginning. I want the data passed to the defined function. I know I may not be explaining this very clearlly, but hopefully somebody understands what I'm talking about. The function is for an xmlHttpRequest with async on, so I'm looking for a way for the endFunction (when the request is all done) to be able to use the xmlHttp object without using global vars.

Thanks a lot.
AluminumPork
Reply With Quote

  #2 (permalink)  
Old Jul 22nd, 2006, 11:15
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Pre-defining a function that can pass variables.

Ok, we need to get this string:
... = Function('callWhenDone(passThisVar)');
do you mean...
... = function(){ callWhenDone(passThisVar); };
Were you just giving passing the idea or actually showing code???


***
You'll need to clarify that before I can help you. I don't know exactly what the situtation is but this might help... (based on what you've given me)

For a string, you could do this:
var testFunction = Function('callMe("'+varToPass+'");');

For a number, you could do this:
var testFunction = Function('callMe('+varToPass+');');

For a dom element, try passing the element id as a string.

***
Another approach would be to, instead of sending the variable as a parameter, send a function as a parameter which would get the current value of the variable you're trying to pass. eg:
... = Function('callWhenDone(LoadTheVarToPass())');

***
I know what the problem with the above is: the variable varToPass will have been updated by the time the function is called. Well, how you solve this will depend on your code's structure / variable scope.

If varToPass is a global variable, then this should work. As far as I now, it's the proper way to do it and it would always execute using the current value of varToPass.

// define function
obj.testFunction = new function(){ callMe(varToPass); };
// call function
obj.testFunction();


***
hope that helps...
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Jul 22nd, 2006 at 11:19.
Reply With Quote
  #3 (permalink)  
Old Jul 22nd, 2006, 19:07
New Member
Join Date: Jul 2006
Location: Minnesota
Age: 21
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Pre-defining a function that can pass variables.

Wow, what a great reply. Yes I was referring to Function('');
Sorry for not clarifying that. But reading through your reply I realized I could
just do var functionToCall = function(varToPass){ realFunctionToCall(varToPass); }

This I can call it like functionToCall(varToPass) and everything gets passed through. Works great and I no longer have to use any global vars, YAY!

Thanks so much for your help!
Reply With Quote
  #4 (permalink)  
Old Jul 24th, 2006, 12:16
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Pre-defining a function that can pass variables.

No worries. It was a good exercise going through the options...
It would ebe great if you could stick around and be part of the forums.
It's very sad when people get help and leave! LOL
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
Reply

Tags
predefining, function, pass, variables

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 On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Pass to Lynda.com simonb Webforumz Cafe 9 Oct 4th, 2007 13:53
Pass Dynamic values in Anchor Tag PraveenAnekalmat JavaScript Forum 0 May 4th, 2007 10:19
Trying to pass JSP variables into input:select attributes. Help badly needed danwms Other Programming Languages 2 Apr 19th, 2007 09:44
Problems with defining class jj1234 Web Page Design 3 Feb 6th, 2006 07:47
How to pass the value from a combo box dinesh Classic ASP 1 Nov 21st, 2005 12:16


All times are GMT. The time now is 01:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43