Five bad habits of writing JavaScript code. Can you see if you have been shot? _ Javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces five bad habits of writing JavaScript code. Can you see if you have been shot ?, This article points out that you have not used namespaces, variables defined in the East and West, Javascript variable ranges, Javascript object-oriented, new keywords, and other issues, if you need it, you can refer to Javascript, which has a bad reputation on the Internet, but it's hard for you to find another language that is so dynamic, widely used, and rooted in our lives. Its low learning threshold makes many people refer to it as a pre-school scripting language. Another thing that makes people laugh at is the concept of Dynamic Language, which uses a high standard of static data. In fact, you and Javascript both stand wrong, and now, you make Javascript very angry. There are five reasons to explain that your Javascript technology is poor.

1. You are not using a namespace.

Do you still remember that the university teacher told you not to use global variables in your homework? The use of global variables in Javascript is no exception. Web pages become messy and messy, and scripts and script libraries are everywhere from every corner of the Internet. If you name a variable loader (), you are looking for trouble. Javascript will not remind you If you reload a function unconsciously. You still call it a preschool education programming language, remember? What I want to say is that you need to know what will happen after doing this.

The Code is as follows:


Function derp () {alert ("one ");}
Function derp () {alert ("two ");}
Derp ();

"Two", the answer is "two ". This is not always the case. It may also be "one ". Therefore, it is easy to put all your code in your namespace. The following is a simple way to define your own namespace.

The Code is as follows:


Var foospace = {};
Foospace. derp = function () {alert ("one ");}
Function derp () {alert ("two ");}
Foospace. derp ();

2. You are performing a magic trick. You define the variable east and west.

Using a combination of numbers, letters, and numbers as variable names is a double-win conclusion. Finding a variable without any ideographic characters in a code block of 40 rows is a nightmare for maintenance. Mixing the first declaration of variables into a 40-line code block is also a nightmare. Even if you encounter such a variable, you may not ask yourself: "Where is this defined ?", Then, we quickly use Ctrl + F to find the initial position of the variable in the source code. No, don't. On the contrary, this is an abuse of Javascript, and it is a stupid way. You should always define the variable at the top of its scope of use. It cannot be said that because it is not necessary, you can not do so.

The Code is as follows:


Function (){
Var a, // description
B; // description
// Process...
}

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

You are a talented programmer. You eat C ++ and pull a List. You know what the range of variables is. You have full control over your variables, and you are watching them like they are too powerful. However, Javascript makes a bubble in your coffee and laughs.

The Code is as follows:


Var herp = "one ";
{
Var herp = "two ";
}
Alert (herp );

In this case, the herp you get is not "one", but "two ". The effective scope of Javascript variables is not dependent on code blocks as in other languages. The Javascript variable range is based on the function. Every function has its own variable range. Javascript is so cool that it ignores the meaningless Scope of the arms. In fact, Javascript is so cool that you can even pass variable ranges like namespaces or variables.

4. You think Javascript's object-oriented features are only grafted.

Javascript has been an object-oriented language since its launch. Everything is an object in Javascript, and everything! Even text symbols such as numbers and characters can be converted into objects through its own constructor. Compared with other object-oriented languages, Javascript has no class ). Javascript objects are defined as functions, and even functions themselves are objects. Javascript has a property called prototype, which is built into all objects. You can use it to change the object structure, modify the object, add more variables, and more functions.

The Code is as follows:


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 seems unrelated to you, I would like to introduce my good friend Google to you. Google is good at helping people learn knowledge. Object-oriented is really a big topic for my short and low-profile article.

5. When you use the "new" keyword, it is like a blind man or a blind man.

Javascript must be your first girlfriend, because you seem at a loss. If you want Javascript to be pleasing like a real person, you need to understand object symbols. In addition to instantiating an object or a rare case where data needs to be loaded at a time delay, you basically do not need to use the new keyword. Allocating a large number of new variable addresses in Javascript is a very slow operation. For efficiency, you should always use object symbols.

The Code is as follows:


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

Do you still remember that the Javascript variable range is function-based? Do you still remember that Javascript objects are defined as functions? If you do not use the new keyword to declare an object, you will make this object a global object. Therefore, it is a good habit to always use the new keyword to declare an object.

The Code is as follows:


Var derp = "one ";
Var Herp = function (){
This. derp = "two ";
}
Var foo = Herp ();
Alert (derp );

If you write it like this, Javascript will not care, and the answer you actually pop up is "two "! There are many ways to prevent objects from making such behaviors. You can use instanceOf, but a better way is to correctly use the new Keyword, which is more professional.

Now you know that your Javascript code is poorly written. If you remember the above, your code will be improved. I like to use three tab keys to indent the code. I like to use underscores to connect words. I like to uppercase the first letter of a function name to indicate that it is an object. Of course, this is another discussion. There are many reasons that may cause your Javascript code to be poorly written, just like I have a lot of bad technologies. So, express your opinions, support, and disagree in comments, please kindly advise.

We are very grateful to rogeliorv and Jared Wein for pointing out the error in the fifth point. You are very strong.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.