![]() |
|
The Internet Web sites, web development, email, chat, bandwidth, the net and society |
![]() |
|
Thread Tools | Display Modes |
|
![]() |
#1 |
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. } 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 |
![]() |
![]() |
![]() |
#2 |
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 |
![]() |
![]() |
![]() |
#3 |
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. |
![]() |
![]() |
![]() |
#4 |
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 |
![]() |
![]() |
![]() |
#5 | |
Abecedarian
Join Date: Oct 2005
Posts: 172
|
Ok, here look at this example (which I know works, I tested it):
Quote:
|
|
![]() |
![]() |
![]() |
#6 |
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 |
![]() |
![]() |
![]() |
#7 |
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
|
![]() |
![]() |
![]() |
#8 |
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 |
![]() |
![]() |
![]() |
#9 |
Abecedarian
Join Date: Oct 2005
Posts: 172
|
Have you tried removing the function wrapper and instead just having:
obj.test = alert("working!"); ? |
![]() |
![]() |
![]() |
#10 |
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 |
![]() |
![]() |
![]() |
#11 |
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.
|
![]() |
![]() |
![]() |
#12 |
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 |
![]() |
![]() |
![]() |
#13 |
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 |
![]() |
![]() |
![]() |
#14 |
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] |
![]() |
![]() |
![]() |
#15 |
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 |
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|