![]() |
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.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? |
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(); |
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. |
I couldn't get that to work...
|
Ok, here look at this example (which I know works, I tested it):
Quote:
|
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(); |
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
|
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(); |
Have you tried removing the function wrapper and instead just having:
obj.test = alert("working!"); ? |
is there any advantage to doing so?
|
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.
|
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.
|
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. '" |
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? |
you haven't even see my code TS, steady on!
|
All times are GMT -5. The time now is 05:46 AM. |
Powered by: vBulletin Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.