Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > JavaScript

Reply
 
LinkBack Thread Tools
Old July 22nd, 2006, 02:51 AM   #1
New Member
 

Join Date: Jul 2006
Location: Minnesota
Age: 23
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 aluminumpork is on a distinguished road
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
aluminumpork is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 22nd, 2006, 07:15 AM   #2
Most Reputable Member
 
spinal007's Avatar
 

Join Date: Mar 2004
Location: Good Ol'London
Age: 24
Posts: 1,684
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Altering Power: 0 spinal007 has a spectacular aura about spinal007 has a spectacular aura about
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...
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON

Last edited by spinal007; July 22nd, 2006 at 07:19 AM..
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 22nd, 2006, 03:07 PM   #3
New Member
 

Join Date: Jul 2006
Location: Minnesota
Age: 23
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 aluminumpork is on a distinguished road
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!
aluminumpork is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 24th, 2006, 08:16 AM   #4
Most Reputable Member
 
spinal007's Avatar
 

Join Date: Mar 2004
Location: Good Ol'London
Age: 24
Posts: 1,684
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Rep Altering Power: 0 spinal007 has a spectacular aura about spinal007 has a spectacular aura about
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
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks

Tags
predefining , function , pass , variables


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 The Café 9 October 4th, 2007 09:53 AM
Pass Dynamic values in Anchor Tag PraveenAnekalmat JavaScript 0 May 4th, 2007 06:19 AM
Trying to pass JSP variables into input:select attributes. Help badly needed danwms Other Languages 2 April 19th, 2007 05:44 AM
Problems with defining class jj1234 HTML, XHTML and CSS 3 February 6th, 2006 03:47 AM
How to pass the value from a combo box dinesh Classic ASP 1 November 21st, 2005 08:16 AM


Search Engine Optimization by vBSEO 3.2.0 RC8