Make five javascript details, so don't let people see that you are bad.

Source: Internet
Author: User

JavaScript has a bad reputation on the Internet, but it is hard for you to find another language that is so dynamic, so widely used, and so 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 have taken the wrong stance, and now, you make JavaSript 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.
[Javascript]
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.
[Javascript]
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. Looking for a variable without any ideographic characters in a code block of 40 lines 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.
[Javascript]
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.
[Javascript]
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 variable range of JavaSript is function-based. 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.
[Javascript]
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.
You must be your first girlfriend because you seem at a loss. If you want to see the object symbol as a real person, you need to understand it. 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 javasrept is a very slow operation. For efficiency, you should always use object symbols.
[Javascript]
Var rightway = [1, 2, 3];
Var wrongway = new Array (1, 2, 3 );

Do you still remember that I said that the variable range of JavaSript is function-based? Do you still remember some people saying that the objects of JavaSript 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.
[Javascript]
Var derp = "one ";
Var Herp = function (){
This. derp = "two ";
}
Var foo = Herp ();
Alert (derp );

If you write it like this, you will not care about it, but the real pop-up answer 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 JavaSript code is poorly written. If you remember what you mentioned 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 lead to poor coding of your JavaSript code, just like I have a lot of bad technologies. So, express your opinions, support, and disagree in comments, please kindly advise.

Author: ymnets

Related Article

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.