The Cellar  

Go Back   The Cellar > Main > The Internet
FAQ Community Calendar Today's Posts Search

The Internet Web sites, web development, email, chat, bandwidth, the net and society

Reply
 
Thread Tools Display Modes
Old 11-13-2005, 10:31 AM   #1
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
Javascript functions...

Here we go again...

I'm playing around with something at the moment, I'm trying to find a way to be able to call pass a function as a paramater to another function, for example:


Code:
var obj = new Object();

obj.test = function() {
alert("working!");
}

obj.run(param) {
param;  //execute param, in theory.
}
so that when you call:
obj.run(obj.test);
obj.test gets executed. Obviously it doesn't work. A bit of playing around had led me to believe it is simply putting the code of the function there instead of running it, whether it's a named function of an anonymous function attached to an object. Anyone got any ideas?
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-13-2005, 10:38 AM   #2
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
Argh, all you have to do is as () where you want the function called
thus to call the param that is a function in the function simply do param();
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-14-2005, 03:06 PM   #3
laebedahs
Abecedarian
 
Join Date: Oct 2005
Posts: 172
It's been a while since I've messed with javascript, but I believe you're making it too hard.

Let's start with the function you want to return the value from first.

function function1() {
statements;
// here's the important part: whatever you want to be able to be used in another function or statement, you need to "return" it
return blahblah;
// so if you have a variable named "blahblah" that you stored some info in, it will return that.
}

// here's the second function

function function2(blah) {
document.write(blah);
}

Now... you can pass the first function to the second function like this:

function2(function1());

You can also pass variables/data to the first function like you normally would. You can also return boolean values, such as TRUE or FALSE.
laebedahs is offline   Reply With Quote
Old 11-14-2005, 03:08 PM   #4
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
I couldn't get that to work...
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-14-2005, 03:22 PM   #5
laebedahs
Abecedarian
 
Join Date: Oct 2005
Posts: 172
Ok, here look at this example (which I know works, I tested it):

Quote:
function test1(blah1) {
return blah1;
}

function test2(blah2) {
return blah2;
}

window.alert(test1(test2('testing')));
laebedahs is offline   Reply With Quote
Old 11-14-2005, 05:07 PM   #6
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
ah wait a minute, yes, but you can't execute it inside the function, can you? the object code is unneeded, i could've done it with normal functions as well, just a personal preference in this scenario.

Code:
var obj = new Object();

obj.test = function() {
alert("working!");
}

obj.run(param) {
//do something interesting here
param;  //execute param, in theory.
//do something interesting here as well
param //maybe call it again.
}
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-14-2005, 06:02 PM   #7
laebedahs
Abecedarian
 
Join Date: Oct 2005
Posts: 172
It looks to me as if you're trying to create a class. I haven't too much experience with those, see: http://www.webdevelopersjournal.com/...js_begin3.html
laebedahs is offline   Reply With Quote
Old 11-15-2005, 11:21 AM   #8
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
kindasortanotreally, there's better ways of doing it though prototype but they're not really needed here and add no real capabilities. I mean I could redefine obj.test as needed and call it directly but unless I need to pass variables with the function, there's no advantage.

Code:
var obj = new Object();

obj.test = function() {
alert("working!");
}

obj.run(param) {
//do something interesting here
param;  //execute param, in theory.
//do something interesting here as well
param //maybe call it again.
}
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-15-2005, 05:34 PM   #9
laebedahs
Abecedarian
 
Join Date: Oct 2005
Posts: 172
Have you tried removing the function wrapper and instead just having:

obj.test = alert("working!");

?
laebedahs is offline   Reply With Quote
Old 11-16-2005, 02:04 PM   #10
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
is there any advantage to doing so?
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-19-2005, 12:13 AM   #11
laebedahs
Abecedarian
 
Join Date: Oct 2005
Posts: 172
Well, if you're not using the variable-passing feature of a function, the advantage is: smaller code. The way you're using a function it is unnecessary to do so.
laebedahs is offline   Reply With Quote
Old 11-19-2005, 12:13 PM   #12
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
in the realworld version of this obj.test would be closer to 100 lines and be used in other places in other ways but i see your point in this exact demo.
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Old 11-21-2005, 11:33 AM   #13
Troubleshooter
The urban Jane Goodall
 
Join Date: Jan 2004
Location: Florida
Posts: 3,012
How To Write Unmaintainable Code

http://developers.slashdot.org/devel...&tid=156&tid=8

An anonymous reader writes "Make sure you're irreplaceable -' In the interests of creating employment opportunities in the Java programming field, I am passing on these tips from the masters on how to write code that is so difficult to maintain, that the people who come after you will take years to make even the simplest changes. Further, if you follow all these rules religiously, you will even guarantee yourself a lifetime of employment, since no one but you has a hope in hell of maintaining the code. Then again, if you followed all these rules religiously, even you wouldn't be able to maintain the code! You don't want to overdo this. Your code should not look hopelessly unmaintainable, just be that way. Otherwise it stands the risk of being rewritten or refactored. '"
__________________
I have gained this from philosophy: that I do without being commanded what others do only from fear of the law. - Aristotle
Troubleshooter is offline   Reply With Quote
Old 11-21-2005, 12:30 PM   #14
Happy Monkey
I think this line's mostly filler.
 
Join Date: Jan 2003
Location: DC
Posts: 13,575
Speaking of Java, on my home PC, I installed the latest version, and now Java applets won't show up in Firefox. The box is empty. Non-browser java works fine. I've uninstalled and reinstalled it through the Firefox plugin manager, and through a desktop installer.

I'm not sure about IE. I should check on that. But in the meantime, has anyone seen something like that?
__________________
_________________
|...............| We live in the nick of times.
| Len 17, Wid 3 |
|_______________| [pics]
Happy Monkey is offline   Reply With Quote
Old 11-22-2005, 10:47 AM   #15
jaguar
whig
 
Join Date: Apr 2001
Posts: 5,075
you haven't even see my code TS, steady on!
__________________
Good friends, good books and a sleepy conscience: this is the ideal life.
- Twain
jaguar is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

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

Forum Jump

All times are GMT -5. The time now is 12:50 PM.


Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.