Click to See Complete Forum and Search --> : Shape and Display of Literal Code


apezVal
04-09-2003, 05:48 PM
I have made great use of the "View-Source" R-click option to study both HTML , JavaScript and other aspects of mark-up for web-development. And I have a few books and all the great online tutorials yet it is still difficult to find all the answers sometimes without asking the pros here.

At least one source has said that the "SHAPE" of the javascript code is critical, and that the lines need to go on to the ';' to end each line (of a Funtion). And when I use View-Source, I find that the code can be sometimes (not always) all shifted to the left- margin while at other times the code is written all across the page, with some lines not starting for long null-strings of white-space and many lines do not align to the left margin. If I try to cut and paste this 'shaped' code, the main word-processor I use always makes it all go to the margins, while Corel WP gives the option to paste with or without formatting. So my question is HOW IMPORTANT is the conservation of code-shape as long as the lines do end in a semi-colon. ??
_JHG

Jona
04-09-2003, 06:00 PM
It makes no difference as long as you code it right:

function do(something){
if(something=="string"){doSomething();}else{doSomethingElse();} }

This function checks to see if the variable "something" matches the string "string." If it does, it calls the function "doSomething()" if it doesn't, it calls the function "doSomethingElse()" You will notice I have semi-colons on there, but that (in this case) makes no difference.

function do(something)
{
if(something=="string")
{
doSomething();
}
else
{
doSomethingElse();
}
}

This is how most people code it. I, again, am using semi-colons, but the above will work without semi-colons as well.

This changes when defining variables:

var x = 0 i = x

This is an error, but this:

var x = 0; i = x

does not return an error. You can also use commas if you want to create multiple undefined variables:

var i, x, y

apezVal
04-09-2003, 06:30 PM
Thank you Jona for the speedy reply, i will not be so concerned about the code "wrapping" in NotePad as long as it is formatted properly. I suspect some of the 'spaced-out' non-margined code I have found may have been machine generated to waste so much space maybe.

Thanks again.
__ JHG

Jona
04-09-2003, 06:32 PM
Actually most people make their code that way. And it seems liek Notepad is to blame, but really it's just their coding. I don't code the way most people do... Everything goes on one line if at all possible (and it is) in my opinion. :)