PDA

View Full Version : clear a few things up for me..


vkidv
10-14-2002, 10:54 PM
im kind of new to javascript, (i can type up simple scripts quite easily, and make some real complex ones) but i dont know a REALLY important element/thing in javascript why/how its done, heres a few things i want answered :D :D
Using the '{' and '}'
I undersand how these work, In functions and 'If's. But i dont know how to have One If in ANOTHER 'if'. Or how to have an 'if' in a function (ive done this many times before, but i dont understand)

I dont undersand the thing when you do this:
}
}
}
I see this on scripts all the time and i think this has somthing to do with this:
function name() { // Do i put the curly bracket on the same line? or after? And if i put it on the next line, do i ident it?*
var hello1="hello"
if (hello=="hello")
{ //i put tis here this time?
<<stuff here>>
}//if i put it here, would the FUNCTION's Ending curly bracket get mixed up with the 'IF's curly bracket?
}


*
function name()
{ //Do i put this lining up with the end one?
} //Lined up with the starting curly bracket

this.<somthing>
I dont completley understand this :confused: ...What does this do? Ive seen this in loads of 'in-html-javascript-event-handlers' like
onclick="this.<somthing><somthing>"
what can the 'this' thing do?
please help IF you understand!

Spookster
10-14-2002, 11:04 PM
Please read the posting guidelines regarding thread subjects before posting any more threads

http://www.codingforums.com/postguide.htm

Mr J
10-14-2002, 11:08 PM
Basically { is the beginning of a statement and } is the end of a statement.

{ must always be at the beginning of a statement but the }'s can be placed at the end of a function.

function one(){
if(i==0){
alert("HI")
}
if(i==1){
alert("HI THERE")
}
}

You can go like this as well

function one(){
if(i==0){
alert("HI")
if(i==1){
alert("HI THERE")
}
}
}

Basically its all down to tidy scripting.

Something like that anyway.

Someone else might be able to go more indepth but I suggest you buy a good javascript book

chrismiceli
10-14-2002, 11:26 PM
you have to use the } to close and the { to open

function hi() { //opens function
if (name=="jon") { //opens if
alert("hi") } //closes if
} //closes function

beetle
10-14-2002, 11:37 PM
Also note....the { and } when used with loops and IF statements are only necessary if you are performing more than 1 operation.if (var == '')
alert('Achtung!');This effect also "nests" so to speak.var a = [1,2,3];
var b = [1,2,3];
var c = [1,2,3];

for (var i in a)
for (var j in b)
for (var k in c)
alert(a[i]+b[j]+c[k]);orvar a = [1,2,3];
var b = [1,2,3];
var c = [1,2,3];
var x = true;

for (var i in a)
if (x)
for (var j in b)
alert(a[i]+b[j]);
else
for (var k in c)
alert(a[i]+c[k]);See? No curly braces!

whammy
10-15-2002, 01:37 AM
But, you replaced the curly braces by entering a new line.

I prefer (to make the script very clear, and this is just my personal pet peeve) to do it like this:


if(something==whatever){
// do something
if(somethingelse==whateverelse){
// do something else
}
}

etc...

I just think it makes it really clear as to what's open and closed, and what the script heirarchy is at the moment. I also do this in VBScript and XHTML... I figure that way if you have to work on your script 6 months down the road, or someone else has to work on it, it will be that much easier to read!


Obviously you agree on indenting the code, at any rate... that's probably the most important aspect. :)

From what I have seen, you may be more knowledgable than me, but although I know they are not necessary, the curly brackets can be a visual aid to program flow - and are also familiar friends which say "Hey, this thing is now OVER!". :)

beetle
10-15-2002, 02:48 AM
Whammy,

All true. In fact, I rarely make such large code structures w/o curly braces (just for this example), but for many 1-liners I just simply leave them out, or put it on one line altogether...

if (var == '') alert('Achtung!');

Cheers, and happy coding! :D

whammy
10-15-2002, 03:16 AM
Yeah I do that too... but it kills me to do it. ;) hehe

beetle
10-15-2002, 03:18 AM
Whammy = slave to the code.

Hehe :D

whammy
10-15-2002, 03:26 AM
You got me man... I think Netscape and IE have brainwashed me into supporting little nitpicking things (like how to indent code, for instance) because of their very annoying use of proprietary code... instead of trying to agree on standards.

That has not only hurt web developers trying to code correctly, it has probably caused many, many an average internet user a very bad user experience.

I'm all for pure XHTML/XML now, after seeing the benefits (not to mention PURE ECMAScript - no proprietary ****).

In a perfect world... ?

Roy Sinclair
10-15-2002, 02:30 PM
Originally posted by whammy
But, you replaced the curly braces by entering a new line.

I prefer (to make the script very clear, and this is just my personal pet peeve) to do it like this:


if(something==whatever){
// do something
if(somethingelse==whateverelse){
// do something else
}
}

etc...

I just think it makes it really clear as to what's open and closed, and what the script heirarchy is at the moment. I also do this in VBScript and XHTML... I figure that way if you have to work on your script 6 months down the road, or someone else has to work on it, it will be that much easier to read!


Obviously you agree on indenting the code, at any rate... that's probably the most important aspect. :)

From what I have seen, you may be more knowledgable than me, but although I know they are not necessary, the curly brackets can be a visual aid to program flow - and are also familiar friends which say "Hey, this thing is now OVER!". :)

I code in a similar fashion except for a couple things,


if(something==whatever)
{
// do something
if(somethingelse==whateverelse)
{
// do something else
}
}


If the closing brace needs a line of it's own, the opening brace should also get the same treatment. Likewise the opening brace starts the indent and the closing brace ends the indention, that makes matching the opening brace to the ending brace much simpler. This is probably an artifact of learning a block structured programming language that used BEGIN and END instead of { and } first but I still think the code looks much better when { and } are treated the same.

I also tend to code this:


if (somesortofcondition)
{
//do something
}
else
{
// do something else
}

instead of:


if (somesortofcondition)
{
//do something
} else {
// do something else
}


The latter is a form I see a lot and it's not too hard to follow but ultimately I prefer to keep a consistent style.

beetle
10-15-2002, 04:09 PM
I used to always doif (somesortofcondition)
{
//do something
}
else
{
// do something else
}But eventually switched toif (somesortofcondition) {
//do something
}
else {
// do something else
}from some crazy obsession with saving lines. To me, the indendation is a good enough visual aide to indicate the start of a block. I agree, however, that the closing brace should end (but include) the indendation...because done this way...if (somesortofcondition) {
//do something
}
else {
// do something else
}Those curly braces look unattached, floating in the vast sea of whitespace that is your editor's backdrop. In larger scripts, this is particularly unnerving. To me, the perhaps most annoying looking style is this (no offense to those here that use it...:D)if (somesortofcondition)
{
//do something
}
else
{
// do something else
}

Mr J
10-15-2002, 06:04 PM
Like I said guys, all down to tidy coding, You know whats what with what

:D