Five reasons why your JavaScript technology sucks. _javascript Tips

Source: Internet
Author: User
Tags variable scope
This article is from the 5 reasons Your Javascript stinks this article translation.

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 do not 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 that you need to know what happens when you do this.

Copy Code code as follows:

function Derp () {alert (' one ');}
function Derp () {alert ("two");}
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.
Copy Code code as follows:

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

2. You are juggling, you define 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 such a variable yourself, you may as well ask yourself: "Where is this defined?" , and then quickly use the ctrl+f combination to look in the source code for the location 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.
function () {
var a,//description
b Description
Process ...
}
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.
Copy Code code as follows:

var herp= "one";
{
var herp= "two";
}
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 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.
Copy Code code as follows:

var Derp; Would 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 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.
Copy Code code as follows:

var rightway= [1, 2, 3];
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 do not use the NEW keyword to declare an object, you will make the object a global scope. So it's a good habit to always use the new keyword to declare objects.
Copy Code code as follows:

var derp= "one";
var herp=function () {
This.derp= "two";
}
var foo=herp ();
alert (DERP);

If you write this, JavaScript doesn't care, and the answer you really pop up is "two"! There are a number of ways to prevent an object from doing such a thing, to 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.
Thank Rogeliorv and Jared Wein for pointing out the mistakes in the 5th. You're very strong.

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.