...

View Full Version : javascript standards



code-in-time
03-25-2012, 05:38 PM
Hi Could you please point me in the right direction when it comes to javascript standards. I have search online and i see that there is no correct way to go-- is this right?



1) is it better to use single or double quotes
var test = 'string';
var test = "string";

$("#test").val();
$('#test').val();

2)do function names start with a upper or a lower case?


thanks

anotherJEK
03-25-2012, 08:38 PM
Double or single quotes depend on whether or not you want a multiline
string


var string1 = "this is a \n multiline string";
var string2 = 'this is not a \n multiline string';
// in some cases in Internet Explorer;
// you will have to use \n\r for new lines


also, if you have the same type of quote inside a quote set:
'here is a string \'with the same type of quote\' inside'
or
'here is a string "with the same type of quote" inside'
or
"here is a string 'with the same type of quote' inside"
further, in English at least 'can't' will not work. Use "can't" or 'can\'t'

for built in functions and methods you will have to refer to a text like O'Reilly's
Javascript: The Definitive Guide. I believe the latest revision is the
5th edition.
IF you write your own functions, it does not matter. Just call them or
refer to them with the same name (including caps and lower case)
that you gave them

Old Pedant
03-25-2012, 08:47 PM
1) is it better to use single or double quotes
var test = 'string';
var test = "string";

$("#test").val();
$('#test').val();


Yes. Definitely. If you use anything else your code won't work. <grin/>

Okay, sorry. Couldn't help myself. It DOES NOT MATTER. Personally, I always use "..." unless the string needs to contain a ", in which case I then use '...'. I know I can use \" to put the " inside of "...." but that looks ugly to me and I only use it as a last resort.



2)do function names start with a upper or a lower case?

Yes. Or an underline. Or a dollar sign. Your choice.

Now... *MOST* people start function names with a lower case letter. But is there a rule about that? No.

Unless you happen to work for some company that has coding standards, of course.

felgall
03-25-2012, 09:42 PM
The built in functions/objects use an uppercase letter to start those that are intended to be used as the basis for creating your own objects (where they'd be classes in an object oriented language) while the built in functions and objects intended to be used as supplied start with a lowercase letter.

This convention is often followed by JavaScript programmers for the functions and objects they create themselves but this is just a convention and there is nothing to force you to do this (unless your complany standards state otherwise).

In the same way there is a convention (but not a standard) to use an underscore on the front of private functions as a reminder that they have limited scope.

The actual JavaScript standards can be found at http://www.ecma-international.org/publications/standards/Ecma-262.htm (for the core portion of JavaScript) and http://www.w3.org/DOM/ (for the document object model part).

The JavaScript validator can be found at http://jslint.com - for JavaScript attached into web pages the only checkbox at the bottom that you need to check is "Assume a browser" so that it doesn't give errors relating to the window object (which only exists when JavaScript is run in a web browser).

Dormilich
03-26-2012, 06:45 AM
further, in English at least 'can't' will not work. Use "can't" or 'can\'t'


or 'canít' (which would be typographically correct)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum