Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-07-2004, 10:29 PM   PM User | #1
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
case of Reserved Words?

I occasionally find the most descriptive name to be a reserved word, so lead it w/ an _underscore, but just realized that capitalizing the first letter is also effective:

Has anyone else done this, and experienced an error therefrom?

- haven't tested all of them yet...
__________________
hmm... ?
swmr is offline   Reply With Quote
Old 05-07-2004, 10:31 PM   PM User | #2
JAVAEOC
Regular Coder

 
Join Date: Oct 2003
Location: SC
Posts: 936
Thanks: 0
Thanked 0 Times in 0 Posts
JAVAEOC is an unknown quantity at this point
I dont know of any reserved word that starts with a capital letter , thats why all of mine start with one

have fun coding
__________________
http://www.bluephoenix.uni.cc/
JAVAEOC is offline   Reply With Quote
Old 05-07-2004, 10:42 PM   PM User | #3
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
I don't know all that much about Javascript, but I do know that I HATE variable names that start capitalized! It's just coding standards I guess, so it's really up to you..

In general (in OO languages anyway), classes begin with a capital, constants are all caps, and the rest (variables and methods/functions are all that's left.. i think) begin lowercased.

Up to you how you want to do it, but capitalizing in Java (not Javascript) might conflict with a class name, like String..

Just my thoughts..

Sadiq.
sad69 is offline   Reply With Quote
Old 05-07-2004, 10:56 PM   PM User | #4
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Yeah, Sadiq has a point. There's a very firmly put into place convention of using lowercase first letter for general variables, object members and namespaces. You use an uppercase first letter for classes, and all uppercase for constants. However, with the exception of constants, how to write the rest varies more between langauges. C/C++ and Java traditionally use camelCase. Ruby, as an example, instead uses under_scores. Some programmers use hungarian notation, while some just use object descriptive names, and others use usage descriptive names. But, the constant/class(or constructor in JavaScript)/others follow that pattern pretty much everywhere, unless you look at languages with a very non-C, non-BASIC syntax - languages such as Scheme, J (no, J has nothing to do with Java. It's a derivative of APL), Assembly.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-07-2004, 10:58 PM   PM User | #5
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
Where are the detailed coding conventions of JavaScript described, anyway?

I haven't learned to ascribe meaning to case, so have no preference, really -- not that I wouldn't like to know why names are presented differently according to their context...

What about element names... does case also apply there?

I'm sure to find out eventually; just wondering if this has been a problem for anyone...
__________________
hmm... ?
swmr is offline   Reply With Quote
Old 05-07-2004, 11:22 PM   PM User | #6
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
functions,variables are generally written in camelCase(don't know any that ain't).
examples:getElementById,className,nextSibling and so on

Classes are written in 1st letter capital.
example :new Array,new Object,new Image.

which makes it sort of odd how JAVAEOC could say he not encountered any such reserverd words.

another advise from a delphi programmer: is that classes start with T before the object names and the object them self is then without a T.
and arguments send to function start with an a.
Garadon is offline   Reply With Quote
Old 05-07-2004, 11:27 PM   PM User | #7
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, the coding conventions for JavaScript are something like this:

- Object members beginning with underscore generally was Netscape specific and you were discouraged to use them.
- Object members beginning with double underscores or triple underscores were Netscape internal members, and should preferably not be used even if you were programming Netscape specific code.
- Constructors use uppercase first letter.
- Anything else use a lowercase first letter.
- You use uppercase letter for the first letter in each word in an identifier, lowercase for the rest. This goes for acronyms and abbreviations as well.
- However, constants use all uppercase and underscore as word separator.
- Functions and methods SHOULD contain a verb while anything else SHOULD NOT.
- There are some common single-letter variable names that are pretty standard:
error - e
event - e
iterators - i,j,k
length - l
horizontal coordinate - x
vertical coordinate - y
regular expression - re
- Microsoft encourages usage of hungarian notation. That means that you prefix identifiers depending on their supposed content type:
a - array
s - string
o - object
b - boolean
i - integer number
(f - floating point number)
n - number
re - regular expression
fn - function
The problem with this approach is that you might want to store data of another type in the same variable.
I personally use hungarian notation when I want to make my code clear for others, since it makes things easier to follow at times.



Another coding convention that has outcristallised for JavaScript is that opening braces in general are placed on the same line as the control structure instead of on a new line, and that the ending brace should be placed at the same level as the control structure, while the contents have an additional indentation level. You most commonly see an indentation of four spaces, though you can find two spaces or a singe tab as well. Three spaces is very unusual, but you might encounter it among some developers from places that uses that convention. You are encouraged to always use a semicolon to close a statement that is not autoterminating. The only places (according to common coding conventions) you may leave them out are when the next non-whitespace character is a closing brace.

As for commenting, use single line comments for code comments, use block comments only for very long running explanatory text or introductory headers. Following these conventions makes it easier to use block comments when testing and debugging the code, so this convention is something that has come out of pragmatism.



Well, I might have forgotten something, but otherwise that should be fairly complete.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 05-07-2004 at 11:33 PM..
liorean is offline   Reply With Quote
Old 05-07-2004, 11:33 PM   PM User | #8
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
Great liorean, thanks a bunch for laying that out!

About the autoterminating statements: How can you tell?
__________________
hmm... ?
swmr is offline   Reply With Quote
Old 05-07-2004, 11:51 PM   PM User | #9
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, in general, they are the blocks or the control structures:

Take the if statement for instance:
Code:
if (condition) statement
Here you have a statement containing a statement. The if statement is autoterminating, because the statement requires termination. Say that the statement is alert('inside the statement'). That statement needs to be terminated, so you add a semicolon after it to terminate it. However, if the if statement was not autoterminated, you would need to add another semicolon to end the if statement, thus giving the following code:
Code:
if (condition) alert('inside the statement');;
However, we already know that when the statement is terminated the if statement is terminated as well, so there's no need for adding a terminating semicolon to it. Thus we end up with just:
Code:
if (condition) alert('inside the statement');
Same goes for if the statement was a block statement. If the if statement wasn't autoterminating you would need to add a semicolon after the closing brace, like this:
Code:
if (condition) {
    statementList
};
Which again is redundant since we already know the if statement must be terminated when the statement is terminated.




That was an example of when closing the inner statment must lead to closing of the outer statement as well. The function definition and the block statment work the other way: when the outer statement is terminated, you know that the inner statement is terminated. Thus:
Code:
function example(){
   statementList
   return true;
}
The red marked semicolon is not needed for termination, since we know that when the outer statement is terminated (as indicated by the closing brace) the inner statement by necessity must be terminated as well.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 05-08-2004 at 12:02 AM..
liorean is offline   Reply With Quote
Old 05-08-2004, 12:12 AM   PM User | #10
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
Oh, I see; would that be true for all JS Statements [if(), which(), for(), etc.], and does that apply only to such statements?

Edit: Looks like you just answered that...
__________________
hmm... ?

Last edited by swmr; 05-08-2004 at 12:15 AM..
swmr is offline   Reply With Quote
Old 05-08-2004, 01:07 AM   PM User | #11
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
sidebar, regarding case:

I noticed the other day--when trying to assign an expando attribute to an element--that case was an important factor.

something like:
element.setAttributeNode(document.createAttribute("Mousedown"));

- worked just fine in IE, but not in Moz.

Several hours later, after trying multiple combinations of different DOM methods, the solution ended up being "mousedown"!
Oh well, all time was not lost--as I learned of more than one way to accomplish the same thing.
__________________
hmm... ?
swmr is offline   Reply With Quote
Old 05-08-2004, 02:07 AM   PM User | #12
swmr
Regular Coder

 
Join Date: Feb 2003
Posts: 638
Thanks: 0
Thanked 0 Times in 0 Posts
swmr is an unknown quantity at this point
... and "thanks" to everyone for their useful input, by the way.

the end.
__________________
hmm... ?
swmr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:14 PM.


Advertisement
Log in to turn off these ads.