SoFunction
Updated on 2025-04-10

5 bad habits of writing JavaScript code. See if you are shot?

Javascript has a bad reputation on the Internet, but it is difficult for you to find another language like it is so dynamic, so widely used, so rooted in our lives. Its low learning threshold has made many people call it a preschool scripting language. Another thing that makes people laugh at is the concept of dynamic language that uses high standards of static data types. Actually, both you and Javascript have taken the wrong position, and now you make Javascript very angry. Here are five reasons why your Javascript technology is terrible.

1. You are not using a namespace.

Do you still remember that in college teachers told you not to use global variables in your homework? The usage of global variables in Javascript is no exception. If you are not careful, you will become chaotic and there are messy scripts and script libraries that are found from every corner of the Internet. If you name a variable loader(), then you are deliberately causing trouble. If you overload a function unconsciously, Javascript won't remind you at all. You also call it a preschool programming language, remember? What I'm going to say is that you need to know what happens after doing this.

Copy the codeThe code is as follows:

function derp(){ alert(“one”); }
function derp(){ alert(“two”); }
derp();

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

Copy the codeThe code is as follows:

var foospace={};
=function(){ alert(“one”); }
function derp(){ alert(“two”); }
();

2. You are making tricks, you define variables one by one.

Your use of inexplicable combination of numbers as variable names is a lose-lose ending. Looking for a character variable without any meaning in a 40-line code block is a nightmare for maintenance. Mixing the first declaration of a variable into a 40-line code block is also a nightmare. Even when you encounter such a variable yourself, you can't help but ask yourself: "Where is this defined?", and then quickly use the Ctrl+F combination to find the initial definition of this variable in the source code. No, don't do this, on the contrary, it's an abuse of Javascript and a stupid approach. You should always define the variable at the top of its scope of use. It cannot be said that because this is not necessary, you can not do so.

Copy the codeThe code is as follows:

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

3. You do not understand the variable scope of Javascript.

You are a genius programmer, you eat C++ and pull List. You know what ranges of variables are, you have complete control over your variables, and you are watching them like the Supreme Emperor. However, Javascript pulls a shit in your coffee and laughs endlessly.

Copy the codeThe code is as follows:

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

In this case you get herp is not "one", but "two". Javascript's variable valid range does not depend on code blocks like other languages. Javascript variable ranges are based on functions. Each function has its own variable range, and Javascript is cool in this way, ignoring the scope wrapped in meaningless braces. In fact, Javascript is so cool that you can even pass variable scopes like namespaces or variables.

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

Javascript, since its inception, has been an object-oriented language. Everything is an object in Javascript, all! Even literal symbols such as numbers and characters can be converted into objects through its own inherent constructor. Compared with other object-oriented languages, Javascript is different in that it does not have classes. Javascript objects are defined like functions, and even functions themselves are objects. Javascript has a property called prototype. This property is built into all objects. You can change the construction of the object, modify the object, add more variables, and more functions.

Copy the codeThe code is as follows:

var derp; //will hold a Herp instance
var Herp= function(){
=”Javascript is cooler than BASIC.”;
}
=function(){ alert(); }
var derp= new Herp();
();

If this seems to have nothing to do with you, I would like to introduce my good friend Google to you, which is good at helping people learn knowledge. Object-oriented is a big topic for my short, low-stance article.

5. When you use the "new" keyword, it's like a blind man and a blind horse.

Javascript must be your first love girlfriend because you seem at a loss. If you want to please Javascript like a real person, you need to understand object symbols. You basically don't need to use the new keyword except when you need to instantiate an object, or in rare cases where data is loaded in a delayed manner. Assigning a large number of new variable addresses in Javascript is a very slow operation, and for efficiency reasons, you should always use object symbols.

Copy the codeThe code is as follows:

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

Do you remember that I said that Javascript's variable range is based on functions? Do you remember someone saying that Javascript objects are defined like functions? If you do not use the new keyword to declare an object, you will make this object a global scope. Therefore, it is a good habit to always use the new keyword to declare objects.

Copy the codeThe code is as follows:

var derp=”one”;
var Herp=function(){
=”two”;
}
var foo=Herp();
alert(derp);

If you write this way, Javascript won't care, and the answer you really pops up is "two"! There are many ways to prevent objects from doing such behavior, which can be used to use instanceOf, but a better way is to use the new keyword correctly, which makes it more professional.

Now you know that your Javascript code is terrible, and if you remember what is said above, your code will improve. I like to use 3 tab keys to indent the code, I like to use underscores to connect words, I like to capitalize the initial letter of the function to indicate that it is an object. Of course, this is another discussion. There are many reasons why your Javascript code is bad, just like I have a lot of skills, so I can express your opinions, support, objection, and give me some advice in the comments.

Thanks a lot to rogeliorv and Jared Wein for pointing out the errors in point 5. You are very strong.