5 bad habits of writing JavaScript code. Have you been shot?

  • 2020-03-30 04:14:21
  • OfStack

Javascript has a bad name on the Internet, but you'd be hard-pressed to find another language as dynamic, as widely used, and as embedded in our lives as Javascript. Its low learning threshold has led many to call it a pre-script language, and one of the other things that makes people laugh at it is that the concept of dynamic languages USES a high standard of static data types. In fact, you and Javascript are on the wrong side of the argument, and now you're making Javascript angry. Here are five reasons why your Javascript skills suck.

1. You don't use namespaces.

Remember back in college when your teacher told you not to use global variables in your homework? The use of global variables in Javascript is no exception. Web pages can become a mess if you're not careful, a mess of intrusive scripts and libraries from every corner of the Internet. If you name a variable loader(), you are asking for trouble. If you overload a function without realizing it, Javascript won't even tell you. You also called it a preschool programming language, remember? What I'm saying is, you need to know what happens after you do this.


function derp(){ alert( " one " ); }
function derp(){ alert( " two " ); }
derp();

"Two." the answer is "two." Not necessarily. It could be "one." So, it's easy to put all your code in your own namespace. Here's a simple way to define your own namespace.


var foospace={};
foospace.derp=function(){ alert( " one " ); }
function derp(){ alert( " two " ); }
foospace.derp();

2. You're juggling. You're defining variables one by one.

Your use of unintelligible alphanumeric combinations as variable names is a lose-lose outcome. Finding a character variable in a 40-line block of code with no meaning is a maintenance nightmare. Mixing the first declaration of a variable into a 40-line block of code is also a nightmare. Even when you come across a variable like this, you have to ask yourself, "where is this defined?" , then quickly use the Ctrl+F combination to find the original location of the variable in the source code. No, don't do that. On the contrary, it's an abuse of Javascript and a stupid way to do it. You should always define a variable at the top of its scope. It's not that you can't do it because it's not necessary.


function(){
var a,//description
b; //description
//process ...
}

3. You don't understand Javascript's range of variables.

You're a talented programmer, you eat C++, you pull lists. You know what a range of variables is, you have complete control over your variables, and you look at them like the emperor. However, Javascript makes a shit out of your coffee and laughs.


var herp= " one " ;
{
var herp= " two " ;
}
alert(herp);

In this case, the herp you get is not "one", but "two". Javascript's effective range of variables is not as dependent on blocks of code as other languages. Javascript's range of variables is function-based. Each function has its own range of variables, and Javascript is cool at this point, ignoring this meaningless bracket bound range. In fact, Javascript is so cool that you can even pass a variable scope like a namespace or variable.

4. You think the object-oriented features of Javascript are just grafted on.

Javascript, since its inception, has been an object-oriented language. Everything in Javascript is an object, everything! Even literal symbols such as Numbers and characters can be converted into objects by their own built-in constructors. Javascript differs from other object-oriented languages in that it has no classes. Javascript objects are defined like functions, and even functions themselves are objects. Javascript has a property called prototype, which is built into all objects, and you can use it to change the structure of objects, modify objects, add more variables, and add more functionality.


var derp; //will hold a Herp instance
var Herp= function(){
this.opinion= " Javascript is cooler than BASIC. " ;
}
Herp.prototype.speak=function(){ alert(this.opinion); }
var derp= new Herp();
derp.speak();

If this doesn't seem relevant to you, I'd like to introduce you to my good friend Google, which is good at helping people learn. Object orientation is too big a topic for this short, low-profile article.

5. You use the word "new" like a blind man.

Javascript must be your first girlfriend because you don't seem to know what to do. If you want to please Javascript like a real person, you need to understand object notation. You rarely need to use the new keyword, except in cases where you need to instantiate an object, or in rare cases where you need to load data late. Assigning a large number of new variable addresses in Javascript is a slow operation, and you should always use object notation for efficiency.


var rightway= [1, 2, 3];
var wrongway= new Array(1, 2, 3);

Remember when I said that Javascript variable ranges are based on functions? Remember when someone said that Javascript objects are defined like functions? If you do not declare an object with the new keyword, you will make the object a global object. So it's a good habit to always declare objects using the new keyword.


var derp= " one " ;
var Herp=function(){
this.derp= " two " ;
}
var foo=Herp();
alert(derp);

If you write like this, Javascript doesn't care, and the answer you actually pop up is "two"! There are many ways to prevent objects from doing this, using instanceOf, but a better way is to use the new keyword correctly, which is more professional.

Many thanks to rogeliorv and Jared Wein for pointing out the error in point 5. You are strong.


Related articles: