Home > Web Front-end > JS Tutorial > Five reasons why your Javascript skills suck_javascript tips

Five reasons why your Javascript skills suck_javascript tips

WBOY
Release: 2016-05-16 18:07:22
Original
827 people have browsed it

This article is translated from the article 5 Reasons Your Javascript Stinks.

Javascript has a bad reputation on the Internet, but it’s hard to find another language that is as dynamic, so widely used, and so rooted in our lives. Its low learning threshold makes many people call it a preschool scripting language. Another thing that makes people laugh at it is that the concept of dynamic language uses high-standard static data types. In fact, you and Javascript are on the wrong side, and now, you are making Javascript very angry. Here are five reasons why your JavaScript skills suck.

 1. You are not using namespaces.

Do you still remember when your teacher in college told you not to use global variables in your homework? The use of global variables in Javascript is no exception. If you are not careful, Web pages will become chaotic, filled with a mess of mutually infringing scripts and script libraries from all corners of the Internet. If you name a variable loader(), you're asking for trouble. If you overload a function without realizing it, Javascript won't remind you at all. You also called it a preschool programming language, remember? What I'm saying is, you need to know what happens after you do this.

Copy code The 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 could be "one". So, it's easy to put all your code in its own namespace. Here's a simple way to define your own namespace.
Copy code The code is as follows:

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

2. You In Jugglery, you put variables defined here and there.
Your use of an inexplicable combination of numbers and letters as variable names is a lose-lose outcome. Finding a character variable with no meaning in a 40-line block of code is a maintenance nightmare. Mixing the first declaration of a variable into a 40-line block of code 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 location where this variable was originally defined in the source code. No, don't do that, on the contrary, it's an abuse of Javascript and a stupid approach. You should always define a variable at the top of its scope. It doesn't mean that just because it's not necessary, you don't have to do it.
function(){
var a,//description
b; //description
//process…
}
 3. You do not understand the variable scope of Javascript.
You are a genius programmer. What you eat is C and what you pull is List. You know what variable scope is, you have complete control over your variables, and you watch over them like a king. However, Javascript poops in your coffee and makes you laugh.
Copy code 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". Javascript's variable scope is not dependent on code blocks like other languages. Javascript's variable scope is based on functions. Each function has its own variable scope, and Javascript is cool about this and ignores the meaningless scope enclosed by curly braces. In fact, Javascript is so cool that you can even pass variable scopes around 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 in Javascript is an object, everything! 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 an attribute called prototype. This attribute is built into all objects. You can use it to change the structure of the object, modify the object, add more variables, and more functions.
Copy code 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 irrelevant to you, I would like to introduce my good friend Google to you, Google Good at helping people learn knowledge. Object-orientation is too big a topic for my short, low-profile article.
5. You are like a blind man and a blind horse when you use the "new" keyword.
Javascript must be your first girlfriend, because you seem at a loss. If you want to delight Javascript like a real person, you need to understand object notation. Except when you need to instantiate an object, or in rare cases where you need to delay loading data, you basically don't need to use the new keyword. Allocating the addresses of large numbers of new variables in JavaScript is a slow operation, and for efficiency's sake you should always use object notation.
Copy code The code is as follows:

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

Do you still remember that I said that Javascript’s variable scope is based on functions? Do you still 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 globally scoped. Therefore, it is a good habit to always use the new keyword to declare objects.
Copy code The code is as follows:

var derp="one";
var Herp =function(){
this.derp="two";
}
var foo=Herp();
alert(derp);

If you do this Write, Javascript doesn't care, and the answer you actually pop up is "two"! There are many ways to prevent objects from behaving like this, you can use instanceOf, but a better way is to use the new keyword correctly, which looks more professional.
Now you know that your Javascript code is terrible. If you remember the things mentioned above, your code will improve. I like to use 3 tab keys to indent code, I like to use underscores to connect words, and I like to capitalize the first letter of a function name to indicate that it is an object. Of course, this is another discussion. There are many reasons why your Javascript code is poorly written, just like I have many technologies that are terrible, so feel free to express your opinions in the comments, support, oppose, and give me advice.
Many thanks to rogeliorv and Jared Wein for pointing out the error in point 5. You are strong.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template