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.
|
|
|
|
|
![]() |
||
Pre-defining a function that can pass variables.
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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 |
|
|
|
||||
|
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. |
|
|||
|
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! |
|
||||
|
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)
|
![]() |
| Tags |
| predefining, function, pass, variables |
| Thread Tools | |
|
|
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 |