![]() |
JS++ cool idea and technique!
Code:
//define globals first.AOP was first developed by a group from Xerox PARC lead by Gregor Kiczales. This lead to the developement of AspectJ, an extension to java. This is why i write this, programs that dont traditionally use this, such as the loosely written javascript, can use AOP to organize projects, and have extensive web applications. Without it, it may remain misunderstood as a "web toy" or a means to only change CSS around for visuals. Think of it this way, HTML/CSS is the GUI interface frontend for javascript. Javascript can even interact with those elements to add a bit more to it. This is what most people seeing javascript doing. Of course, with this article i havent written any rough drafts or anything I simply threw it down.. if its not up to par with anything just let me know.. send me some links, and i will most likely update it. thanks. |
Doesn't makes any sense.
|
thats fine, im just putting it here in case people do see it...
what part of it confuses you? perhaps i can help? |
OK, who invokes main()?
I hate to say it, but this idea is hardly new and revolutionary. JavaScript developers do this kind of stuff every day. We even have code that wraps original functions so that other functions can run before or after a target function. This is a concept known as aspect-oriented programming, and JS has supported it from the very beginning. |
well its meant to be invoked by either <body onload="main()"> or if one really wishes, onclicks...
but i hardly doubted that anyone HASNT used it before... especially programmers coming from c++, but i just thought id make it a thing... a technique rather than something that just naturally happens.. as for aspect-oriented programming, i should brush up on my history a bit! ive been programming for a long time but never decided to look for any REAL history in it.. so this idea of aspect-oriented javascript programming DOES nullify this article. i will modify it to work correctly with the implimentation! |
Quote:
onload is also unnecessary except if your script needs to confirm if all the images have loaded. For simple interactions with the page simply attaching the script at the bottom of the page results in a lot less code and the script running a lot sooner. In addition, global variables are almost completely unnecessary in JavaScript - the only time they are needed is to provide a way to reference a library that is in a separate file. For stand alone scripts that are not dependent on a library the only use that global variables have is to possibly clash with other scripts to break the functioning of the page. Also alert() is for debugging purposes only and should never be used in a published script. Defining names functions rather than simply assigning anonymous functions to variables also limits your ability to use functions as flexibly as JavaScript allows but with the types of scripts that JavaScript beginners produce this doesn't really matter. Effectively there is nothing whatsoever in your code that should be used in writing JavaScript for modern browsers - except for beginners using that less flexible way of defining their functions. |
Quote:
main(); and your done.. as for the global variables, i understand, but they still do exist.. so i added them into the example.. and for an alert(), i understand this as well, but this example is not based on what the program does, its about a technique for writing it. so maybe this were for debugging purposes? its just to show whats supposed to go through the app. i do appreciate the input however! |
for the code shown, in modern browsers,
Code:
var myGlobal1 = document.getElementById('myGlobal1');Code:
|
A better way to do this would be to use modern JavaScript instead of that antiquated 20th Century version that's been obsolete for about a decade now.
Here's what the OPs code would look like of coded in the right order - as the OP included statements to show the order they go in I have included the same statements but put them in the correct order. What the statements actually do and what real code replaces the alerts would be irrelevant to the order of the statements. Code:
//don't define globals at all |
Quote:
however this is a different technique entirely.. this is with AOP, and its a little different in total. Im sure my writing will be obsolete (already is, but just as an example) in a few years when they say that you must declare the function before anything. the thought that my globals are being garbage collected help a lot, i now understand why some of my programs are simply not working. i will not be declaring global variables again anytime soon! lol |
Quote:
For example of you are going to define an addEvent function that uses an eventListener for JavaScript and attachEvent for JScript then you have to define the two functions the way I did because the way you defined the functions is not allowed inside of if/else. See http://javascriptexample.net/events01.php for an example of this. If you define named functions then those functions are automatically hoist to the top of the code and multiple definitions of the same function will overwrite one another leaving the last version of the function as the one that always gets called. Since they are hoist to the top it would be less confusing if you actually defined them at the top. Quote:
JavaScript declares all the variables in one go so it makes the most sense to use a single statement to declare them. When you work with lots of different programming languages it is easier to work with them all if you base your coding styles on how they actually work - that way they work the way your code reads in each case. |
understood.. im just saying i dont like it :P
however.. like i said before, this is just an idea, and a different way of looking at it.. its not by any means the only way and/or the way that needs to be done.. technically this is the way it SHOULDNT be done.. but im just saying it still works.. |
Quote:
|
Quote:
|
Quote:
Code:
Code:
function test(x,y){i can see how with function hoisting and all one might think it wouldn't work, but it's neat how it works as expected, huh? I for one, strongly recommend using function statements instead of function expressions, it's one less name to type, and gives all functions an internal name. This is important since arguments.callee is likely going away soon. When you pre-declare the var, and then later assign the function without an internal name, recursion is slower and more dangerous, and you have more boilerplate between the function opener and the core logic. That's harder to skim, even if it is more detailed information. if i were to code the OP's syntax, i would DRY all vars and assumed globals, publish the workhorse for later re-use, make it async and dependency self-aware, self-waiting for all depends, and use better function declaration to reduce clutter, allow recursion, and facilitate debugging: PHP Code:
|
| All times are GMT +1. The time now is 10:59 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.