PDA

View Full Version : concept to avoid multiple script problems



djr33
03-22-2007, 10:34 AM
I was just thinking about a script (the recent slideshow posted in the submitted section), and trying to think about a solution to allow, without a lot of recoding, for the script to have multiple instances.
This is very true of many scripts; users want more than one, and doing so would cause problems due to variable names and such.

So, the idea that we all know about is renaming the variables and names/id's of the elements.

However, this is something of a pain.

Is it possible for javascript to generate javascript?

One could simply make the entire script a string within a host script, then change the variable names upon each creation.

Short of this, the solution I could see if PHP in that the javascript would, upon each request for an instance of the script, call the javascript at "myscript.php" (as an external .js file), using a get variable to specific a prefix (or suffix) to the variables, allowing multiple instances.

Basically, the idea is an instance generator. This could be a very helpful improvement to many scripts and an easy way to solve the frequent questions regarding this issue. It would also allow for no specific recoding of the scripts, but just a quick analysis as to which variables etc should be altered to allow for multiple versions, then that could be built into a standard javascript (or if need be PHP) framework to generate these.

Thoughts?

jscheuer1
03-22-2007, 11:06 AM
Actually, this is what the 'this' keyword in Object Oriented javascripting is all about. To oversimplify things, instead of:

var something=whatever;

you use:

this.something=this.whatever;

As I say, it is an oversimplification and generally requires you to also create a master function with prototype functions. Due to coding requirements, other identifiers must sometimes be used in place of the 'this' keyword. The prototype functions are shared by all instances but their threads are kept separate.

Have a look at the code here:

http://home.comcast.net/~jscheuer1/side/files/conveyor_oo.htm

Compare it with the code here:

http://www.dynamicdrive.com/dynamicindex14/leftrightslide.htm

djr33
03-22-2007, 11:32 AM
Interesting, and that makes sense.

But then why is it so hard to help those who want multiple instances? It just seems like my solution is quite simple and requires no changes except a one-time creation of a generation system, then just picking which variables should be changed each time.

To me, it sounds a lot more simple, but this is do to my unfamiliarity with the above method. It certainly sounds fine, if it works, but also seems, due to the difficulty that seems to exist in making scripts work with multiple instances, that another method might be helpful.

jscheuer1
03-22-2007, 01:45 PM
Well, I've had some experience doing these types of conversions. That one went fairly smoothly and I think it took me two hours. It could have been faster. I wasn't working on a deadline. I certainly wasn't being a slow poke about it.

Now, I learn things mostly by doing so, it would be difficult for me to teach this 'conversion method' yet as, even though I've done it about ten times, that isn't enough for me to quite have it down to a formula.

Depending upon the script, it can be a relatively simple or a quite complex operation.

There may well be a simpler method, I've often thought that there should be. But, I haven't found one yet.

djr33
03-25-2007, 08:11 AM
Well, here's what I got for this script using this method--
http://www.dynamicdrive.com/forums/showthread.php?p=83081#post83081

View Here:
http://www.ci-pro.com/misc/phptest/jsinstance/2slideshows.htm

Please move to that thread for specific discussion of that script, obviously.

But discussion of the method is fine here.

It works. I can't say it's pretty, yet, but I think with some work it would be great. Just needs a bit more automation (nothing that wouldn't be needed if you were using the 'this' method), and it should be easy.

Twey
03-25-2007, 12:01 PM
It's certainly possible, but it's not very nice. For a large script, that involves packing the whole thing into a string, including escaping quotes and things, and replacing all variable names with dynamically-generated ones, which is one heck of a lot of slog-work. There are also problems with the finished result: code reuse is one of the biggest no-nos in software design, and is, in fact, the whole reason that functions were invented. To have fifteen different instances of a script in the page, it would have to be written out fifteen times. Especially when factoring in the effort involved in implementing a "generator," it's really much easier and neater just to properly object-orient the script.

djr33
03-25-2007, 12:53 PM
I can see your points. However--

1. There's no escaping needing. Note the PHP posted in the other thread. I'm using the <<<EOF .... EOF; method.

2. The code, whether by function, explicit text-existance or a work of God, will exist in full in the sense that it is running in your system. 30 instances of a function or 1 instance running 30 times will have the same effect, with the difference of a few kilobytes that are kinda annoying but won't even slow down a 56k modem. Additionally, this is NOT explicitly written out, but rather a function in PHP to generate the instances, able to be called dynamically. This may feel like more generated, but it's the same as would be running in the memory if it were a function and also no more text to deal with for the programmer, just the php generator and one instance of the code, using {$x+} ($x is the variable of the for loop) that allows each variable name to be unique.

3. Sure, there's effort involved in a generator, but there's also effort involved in converting. The generator may be tedious, but the conversion of a javascript function to an object oriented method could be hard and requires knowledge of more than I know about javascript, certainly. This may be easy for you, but, if it is, then why all the complaints when someone asks for an OO script version of something? The generator has potential as a generalized solution with all it requires is someone to look through and find all the variable/function names that need to be individualized. That took very little time compared with the rest of it, from setting up the for loop correctly to setting up the more intricate details like the onLoad call and the CSS. Once those things get a little simpler as I play with the script more, I see it having a lot more use.

Bob90
03-25-2007, 01:21 PM
Really OO is all you need. I try to make all my code work with multiple instances (If I think necessary) and it really shouldn't be that hard. All this PHP malarky will get you no-where; surely for each extra call to the dynamic function you add the size of the javascript to the page. I know it's a trivial size nowadays, but PHP it isn't elegant; its a long winded solution.
Well, thats what I think anyway.
:)

Twey
03-25-2007, 01:30 PM
1. There's no escaping needing. Note the PHP posted in the other thread. I'm using the <<<EOF .... EOF; method.You're right, sorry.
2. The code, whether by function, explicit text-existance or a work of God, will exist in full in the sense that it is running in your system. 30 instances of a function or 1 instance running 30 times will have the same effect, with the difference of a few kilobytes that are kinda annoying but won't even slow down a 56k modem.But not here. One function running multiple times is one function running multiple times: the function need only be created once, and exists only once in memory. Thirty functions exist thirty times, and take up thirty times as much memory.
3. Sure, there's effort involved in a generator, but there's also effort involved in converting. The generator may be tedious, but the conversion of a javascript function to an object oriented method could be hard and requires knowledge of more than I know about javascript, certainly. This may be easy for you, but, if it is, then why all the complaints when someone asks for an OO script version of something?Not easy, no: it requires a complete rewrite of the script. However, it would probably, for a small, simple script, take roughly the same amount of time as going through said script looking for each and every variable instance, and the end result would be much neater and more efficient.

Bob90 is completely correct, as well: it's just an ugly solution. It's slow, big, and hackish, using slog work over neat, imaginitive and efficient thinking.

jscheuer1
03-25-2007, 03:20 PM
I'm with Twey and Bobo9 here. When I left the door open for you to come up with your own 'better' solution this is not at all what I had in mind. I'm reminded a little of the child with a toy hammer, suddenly everything needs hammering. You seem to be (in this case at least) like that, only your hammer is PHP. I do admire your command of that language and wish that I had even half as much but, this is clearly not the way to go here - if for no other reason than it presents another language dependency. With javascript you depend upon its being available, if you include PHP for modularity, you are limiting its utility to servers with PHP and clients with javascript. Since modularity is, relatively speaking easy enough in javascript and, you already require that language, you should stick with it so as to have only one
'language lag'. I also agree with the criticism about the multiple versus reusable functions.

jscheuer1
03-25-2007, 04:17 PM
I just thought I would try to add something constructive here. Try out this page:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>OO_Tutor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function oo_alert(txt){
if(!oo_alert.ar)
oo_alert.ar=[];
oo_alert.ar[oo_alert.ar.length]=this;
this.txt=this.txt? this.txt : txt? txt : null;
if(this.txt)
this.doit();
}

oo_alert.prototype.doit=function(){
alert(this.txt);
}
</script>
</head>
<body>
<script type="text/javascript">
new oo_alert('Hey Bob');
</script>
<script type="text/javascript">
var arnie=new oo_alert();
arnie.txt='Hey Arnie';
arnie.doit();
</script>
<script type="text/javascript">
oo_alert.ar[0].doit();
</script>
<script type="text/javascript">
new oo_alert();
oo_alert.ar[2].txt='Hello World';
oo_alert.ar[2].doit();
</script>
</body>
</html>

It is (hopefully) a simple enough example and the four scripts in the body show many of the various ways OO scripts can be used. I will perhaps make up more complex examples in the future.

Twey
03-25-2007, 06:16 PM
Completely unrelated, but this:
this.txt=this.txt? this.txt : txt? txt : null;can be simplified to this:
this.txt = this.txt || txt || null;

jscheuer1
03-25-2007, 06:43 PM
Completely unrelated, but this:
this.txt=this.txt? this.txt : txt? txt : null;can be simplified to this:
this.txt = this.txt || txt || null;

Apparently, even this would suffice:


this.txt=this.txt||txt;

Twey
03-25-2007, 07:34 PM
Yes, but the effect would be different: if this.txt and txt were both falsy (including undefined), it would end up with the value of txt, rather than null.

djr33
03-25-2007, 10:14 PM
But not here. One function running multiple times is one function running multiple times: the function need only be created once, and exists only once in memory. Thirty functions exist thirty times, and take up thirty times as much memory.Sorry. What I mean is that it takes just as much processing. I don't really see the memory usage of the text (in terms of RAM) as too much of an issue, as it's only a few KB. Maybe I'm wrong. But it does take the exact same processing power, which is what I was concerned with. Not sure if this is what you meant though.


Twey, I think this is quite imaginative, actually. It might be wrong, but it's creative, considering that you guys are all suggesting the same thing and I'm suggesting something new/different, and a different approach from standard. This is how things improve, with thinking outside the box. I'm not claiming this is a magic solution, but, hey, it works, and I don't see you rewriting the script ;)

Yes, John, I'm much better at PHP than javascript. However, the php is just used in the background here, and I don't know if even you could do this (without actually rewriting the script) using javascript to generate javascript. Seems like you could use something like the PHP exec() command, repeating instances of the script, but seems like more of a pain and more processing on the user's machine.

"PHP" dependancy is a small issue. It does require that it work on the server. But it's no harder for someone to use than just uploading a standard .js file. Note that I named it .js.php to make it seem familiar to people.
I agree that it would be a pain to teach each user to use PHP, but it's automatic, once the PHP file it setup. The php is also simple and requires no testing with multiple browsers.


My main point here is the number of scripts that people want to exist twice or more on a page and neither of you guys is particularly interested in making it work for all of these scripts, and it requires a lot of knowledge of JS to make it work. With my basic framework, after more testing, I can make it work quite easily with, again, just identifying the variables in each script, then using the same framework.


On that last post, John, I'm not sure what the point is. I understand that it can be used. That's great. But... this is also another solution. I never said yours doesn't work.


I guess, mostly, that I just am presenting this as a hack in the case that it is not, or not yet, converted to OO.

jscheuer1
03-26-2007, 06:22 AM
I may have been too harsh here. Will this work with virtually any script with little or no configuration? If so, it could be a handy tool. If it requires a good bit of configuration. Keep it to yourself, under your hat, in your back pocket, save it for a rainy day, and may your conscience be your guide (oops, that last one doesn't belong here).

Really though, you can and I am sure you will do whatever you like with it. As far as my other post, the oo_alert script, I was just trying to be helpful in further introducing you to the OO approach so as that at some point you might be able to make beneficial use of it for yourself and/or others.

Twey
03-26-2007, 06:39 AM
Twey, I think this is quite imaginative, actually. It might be wrong, but it's creative, considering that you guys are all suggesting the same thing and I'm suggesting something new/different, and a different approach from standard. This is how things improve, with thinking outside the box. I'm not claiming this is a magic solution, but, hey, it works, and I don't see you rewriting the script ;)Unfortunately only due to lack of time to do so :(
It is (kind of) imaginitive, but it's certainly not a step forward. It seems to me that the logical next step after this is to abolish functions entirely, and replace them literally with their code wherever they occur :)

djr33
03-26-2007, 06:52 AM
But it's creative by definition, in that I'm not stepping forward, but to the side. Whether that path will be shorter to the "end", no clue, but, hey, it IS creative. It's also creative to walk on your hands, but it doesn't get you anywhere faster. But it's creative ;)
(This isn't getting us anywhere :p)

John, thanks for what you said. And that's exactly my point. I completely get that OO is a "better" solution, but this is easier for me at least.
No, it is not yet setup to deal with any script automatically, but the basic framework could be applied quickly.
Mainly, as I said, I haven't perfected anything yet certainly, so I need to keep working until it is a bit easier to use. The idea of setting it up like this is quite simple to duplicate without much trouble, just a bit of work.
Again, it needs to be explored more to see if it actually does save time.

If it were possible to write or find a prewritten javascript parser that could automatically search the code then know where to add the count variable at the end of function names.


Another thought that I have had here is based on the way that the script I was testing this on is written. It uses subfunctions within a main function. Then only the outermost function needs to be changed. As such, would it not be possible to make a master function to hold everything and save yourself a bunch of work?
Seems like something could be gained from this.


And John, if the OO above is just an example, thanks. I'll take a look.