Jump to content

Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  styleseye : (24 April 2014 - 02:29 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  styleseye : (24 April 2014 - 02:28 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  yordan : (21 April 2014 - 09:11 PM) Hey, Ritesh, Did You Hear Me?
@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!

- - - - -

Good Javascript Practices Some I understand, some I dont

No replies to this topic

#1 FirefoxRocks


    Super Member

  • [HOSTED]
  • 988 posts
  • Gender:Male
  • Location:Ontario, Canada
  • myCENTs:92.36

Posted 13 August 2009 - 07:01 AM

JavaScript is a very lenient language. It allows you to use variables without declaring/initializing them first. Even though it is object orientated, quite a few JavaScript programmers do not know about the object orientated aspect of JavaScript (this includes me). Furthermore, JavaScript is used in almost all browser-based applications. It is muscle to power the page (the skin and bones are CSS and HTML).

1. Avoid global variables

One of the biggest problems in JavaScript is global variables. They may be convenient, but once you have 50 functions defined, you will find them to be harmful. Since they can be accessed by any function, they can potentially cause problems by interfering (colliding/overriding each other). Read more about it at Yahoo! Developer Blog.

Now I'm not saying that you must not use global variables, but do avoid them. I still haven't found a way to get rid of all globals because they are essential sometimes (or maybe I'm not that good at JavaScript yet).

2. Declare variables before you use them. And functions too

This one is obvious. If you haven't declared them, the JavaScript parser will not know what they are. This goes for functions too. If you need to call a function within a function, don't write it afterwards, write it before, so here is an example:
function def(number) {
[tab][/tab]   // do some stuff
[tab][/tab]  return true;
   function  abc() {
[tab][/tab]  document.write(def(i));[tab][/tab]
In the previous example, declare def() before abc() because you will be using it in abc().

3. Use semicolons.

JavaScript allows you to omit semicolons at the end of statements. Not a good idea. It is good practice to use semicolons because they are required in PHP, C, etc. This is easy. End each statement with a semicolon, this includes function calls and variable declarations. The only exception to this is function declarations, and if/for/while/switch statements.

4. Comma usage

Use commas as a separator only (and of course in strings). Also, omit the last comma in a group of stuff in arrays, objects, etc. as the extra comma will likely cause problems in Internet Explorer and/or other interpreters.
var cars = new  Array("Dodge Ram", "Ford Mustang", "Toyota  Yaris");
   // note that there is no comma after the last  item

5. If you can, use only 1 var statement per function.

This one was tricky at first, but now I'm getting the hang of it. Maybe it's to save space, maybe it's for some other reason. Instead of writing this:
function doSomething() {
[tab][/tab]   var i = 0;
[tab][/tab]  var j = 5;
[tab][/tab]  var k = 61;
[tab][/tab]   // some stuff....
You can do this instead:
function doSomething() {
[tab][/tab]  var i = 0,  j = 5, k = 61;
[tab][/tab]  // some stuff...

6. Encapsulate blocks with { and } even if they are 1 line.

You may see if statements do this (and maybe even while and for):
if(x!==3) return false;
At first you may see that it returns false, but in a long line of code it may not be obvious. Use the curly braces each time:
if(x!==3) { return  false; }
It prevents mistake if you are adding code later on.

7. Do not declare function arguments again in the function as variables.

Don't do this:
function showMe(blocks)  {
[tab][/tab]  var blocks = "...";
It is unnecessary and can produce unexpected results.
Also, make sure that variables are only declared once. You can set a null value to them if you wish.
Another note to add here is that variables should be declared at the top of the function, not within the if/else/for/while statements, unless you are only using them within that block.

8. if(a = :( ... we aren't assigning a to be equal to b
It is likely that in the above statement, we are comparing a and b. Use the == operator to compare values. This goes for while and for statements as well.

9. Use === to compare instead of ==

=== compares the type also, to further increase security and to reduce errors.

"0"===0 returns false, but "0"==0 will return true. Strings are not integers (numbers).

10. Avoid ++ and --

Now this is one that I don't understand. Apparently it is a security issue to use ++ and -- increment operators. It is however easy to fix: i++ just becomes i=i+1.

11. new Array() isn't efficient

Instead of writing:

var cars = new Array("Dodge  Ram", "Ford Mustang", "Toyota Yaris");

Write this instead:
var cars = ["Dodge Ram",  "Ford Mustang", "Toyota Yaris"];

This is also less typing :D

12. with and eval statements are evil

I'm not going into further detail here. With statements make code hard to understand when you are revising it. Eval is probably the result of bad coding or beginner's programming (it is very conveinient, maybe too conveinient).

So there are 12 tips to writing better JavaScript. I have applied almost all of them to my code, it is so much better now! To validate your JavaScript code, use JSLint. Click on "The Good Parts". Also, more information can be found here: http://www.jslint.com/lint.html

Edited by FirefoxRocks, 13 August 2009 - 07:02 AM.

Reply to this topic


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users