Home >Web Front-end >JS Tutorial >5 bad habits when writing JavaScript code, see if you get shot?
This article mainly introduces 5 bad habits of writing JavaScript code. See if you get caught? , this chapter points out issues such as your failure to use namespaces, variable definitions, Javascript's variable scope, Javascript's object orientation, new keyword, etc. Friends who need it can refer to it.
Javascript has a bad reputation on the Internet, but it is 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 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.
function derp(){ alert(“one”); } function derp(){ alert(“two”); } derp();
"two", the answer is "two". It doesn't necessarily have to be this way, it could also 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.
var foospace={}; foospace.derp=function(){ alert(“one”); } function derp(){ alert(“two”); } foospace.derp();
2. You are doing magic, you define variables one by one.
Using 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 don’t understand Javascript variable scope.
You are a genius programmer. What you eat is C and what you do 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.
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.
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 is 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.
var rightway= [1, 2, 3]; var wrongway= new Array(1, 2, 3);
是否还记得我说过Javascript的变量范围是以函数为基础的?是否还记得有人说Javascript的对象像函数那样定义?如果你不使用new关键字来声明一个对象,你将会使这个对象成为全局范围内的对象。所以,永远使用new关键字来声明对象是一个好习惯。
var derp=”one”; var Herp=function(){ this.derp=”two”; } var foo=Herp(); alert(derp);
如果你这样写,Javascript并不会在意,而你真正弹出的答案是“two”!有很多方法可以防止对象做出这样的行为,可以以使用instanceOf,但更好的方法是正确的使用new关键字,这样显得更专业。
现在你知道你的Javascript代码写的很烂了吧,如果你记住了上面所说的东西,你的代码就会有所改善。更多相关教程请访问JavaScript视频教程!