Revealing your JavaScript technology sucks five performance

Source: Internet
Author: User
Tags define functions variables range variable variable scope

JavaScript has a bad reputation on the internet, but it's hard to find another language like it's so dynamic, so widely used, so rooted in our lives. Its low learning threshold makes many people call it a pre-school scripting language, and another thing that makes people laugh is the notion of a dynamic language that uses a high standard of static data types. In fact, you and JavaScript are in the wrong position, and now you're making JavaScript very angry. Here are five reasons why your JavaScript technology sucks.

1. You don't use namespaces

Do you remember that in college the 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 cluttered with a bit of inattention, littered with messy, interactive scripts and script libraries from every corner of the Internet. If you name a variable loader (), you are asking for trouble. If you overload a function in an unconscious situation, JavaScript does not remind you of it at all. You call it a preschool programming language, remember? What I'm saying is, you need to know what happens when you do this.

 
  
  
  1. function Derp () {alert (' one ');}
  2. function Derp () {alert ("two");}
  3. Derp ();

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

 
  
  
  1. var foospace={};
  2. Foospace.derp=function () {alert (' one ');}
  3. function Derp () {alert ("two");}
  4. Foospace.derp ();

2. You're juggling, you're defining the variables of the East one West one

You use an inexplicable number of alphanumeric combinations as variable names to be a double loser outcome. Looking for a character variable in a 40-line block of code that doesn't take any form is a nightmare for the maintenance effort. Mixing the first declaration of a variable into a 40-line block of code is also a nightmare. Even if you encounter a variable like this, you may as well ask yourself: "Where is this defined?" and then quickly use the ctrl+f combination in the source code to find out where the variable was originally defined. No, don't do this, on the contrary, it's a misuse of JavaScript, it's a stupid thing to do. You should always define a variable at the top of its use. And can't say that because it's not necessary, you can not do it.

 
  
  
  1. function () {
  2. var a,//description
  3. b Description
  4. Process ...
  5. }

3. You don't understand the scope of JavaScript variables

You are a gifted programmer, you eat the C + +, pull is the list. You know what a variable range is, you have complete control over your variables, and you look at them like overlord. However, JavaScript is pulling a crap out of your coffee and laughing.

 
  
  
  1. var herp= "one";
  2. {
  3. var herp= "two";
  4. }
  5. alert (HERP);

In this case you get the herp is not "one", but "two". The valid range of JavaScript variables is not as dependent on code blocks as in other languages. The variable scope of JavaScript is based on functions. Each function has its own range of variables, JavaScript is cool at this point, ignoring the meaningless curly braces wrapped up in the range. In fact, JavaScript is so cool that you can even pass a variable range like a namespace or a variable.

4. You think JavaScript's object-oriented features are just grafted on

Javascript, since it was born, is an object-oriented language. All things are objects in JavaScript, all of them! Even the literal symbols such as numbers and characters can be transformed into objects by their own intrinsic constructors. Unlike other object-oriented languages, JavaScript differs in that it does not have classes (class). JavaScript objects are defined like functions, and even functions themselves are objects. JavaScript has an attribute called prototype, which is built into all objects that you can use to change the structure of objects, modify objects, add more variables, and more.

 
  
  
  1. var Derp; Would hold a Herp instance
  2. var herp= function () {
  3. This.opinion= "Javascript is cooler than BASIC.";
  4. }
  5. Herp.prototype.speak=function () {alert (this.opinion);}
  6. var derp= new Herp ();
  7. Derp.speak ();

If this seems to be irrelevant to you, I would like to introduce my good friend Google to you, Google is good at helping people learn knowledge. Object-oriented is a big topic for my short, low-profile article.

5. When you use the "new" keyword, it's like a Mashi.

JavaScript must be your first girlfriend, because you seem to be in a quandary. If you want to entertain JavaScript like a real person, you need to understand the object symbol. You basically don't need to use the New keyword except when you need to instantiate an object, or if you have a rare case where you need to delay loading data. Assigning a large number of new variable addresses in JavaScript is a slow operation and you should always use object symbols for efficiency purposes.

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

Do you remember when I said that JavaScript's variable scope is based on functions? Do you remember someone saying that JavaScript objects are defined like functions? If you don't use the NEW keyword to declare an object, you will make the object a global scope object. So it's a good habit to always use the new keyword to declare objects.

 
  
  
  1. var derp= "one";
  2. var herp=function () {
  3. This.derp= "two";
  4. }
  5. var foo=herp ();
  6. alert (DERP);

If you write this, JavaScript doesn't care, and the answer you really pop up is "two"! There are many ways to prevent objects from doing this, and you can use instanceof, but the better way is to use the new keyword correctly, which is more professional.

Now you know that your JavaScript code is rotten, and if you remember what it says, your code will improve. I like to use the 3 tab key to indent the code, I like to use an underscore to connect the words, I like to capitalize the first letter of the function name to indicate that it is an object. Of course, this is another discussion. There are a number of reasons why your JavaScript code is bad, like I have a lot of technology is rotten, so, heartily in the comments to express your opinion, support, opposition, generous enlighten.

Original link: http://www.aqee.net/2011/04/13/5-reasons-your-javascript-stinks/

"Edit Recommendation"

    1. Nine amazing HTML 5 and JavaScript experiments
    2. 8 Web sites for stunning JavaScript effects
    3. JavaScript Learning notes have questions and answers
    4. 10 amazing HTML5 and JavaScript effects
    5. 20 Creepy JavaScript navigation techniques
"Responsible editor: Chen Yu new TEL: (010) 68476606"


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.