日期:2014-05-16 浏览次数:20374 次
Javascript gets a bad rap on the Internet, but there are few languages that are so dynamic, so widespread, and so deeply rooted in our lives as Javascript is. The low barrier of entry leads some people to call it a script kiddie language, others scoff at the concept of a dynamic language while riding their statically typed high horse. You and Javascript just got off on the wrong foot, and now you've made it angry. Here's five reasons why your Javascript code sucks.
Remember in college when the teacher said you can't use global variables in your homework? Using globals in Javascript is no different. Web pages tend to be soups of aggressively pasted scripts and modules from every corner of the Internet. If you're using a variable named loader() you're just asking for a Javascript smack down. If you unwittingly over write a function, Javascript's not going to complain. You called it a script kiddy language, remember? That means you know what's going to happen when you do this.
function derp() { alert("one"); } function derp() { alert("two"); } derp();?
Two, the answer is two. It didn't have to be. It could have been one. Namespacing all of your code is good manners, and it's easy to do. Here's an easy way to namespace.
var foospace={}; foospace.derp=function(){ alert("one"); } function derp(){ alert("two"); } foospace.derp();?
Using magic numbers is a no-no. Finding a seemingly arbitrary number in the middle of a 40 line block of code is a maintenance nightmare. Declaring a variable for the first time in a 40 line block of code is just as much of a scare. When you come across one you may ask yourself, "where was this declared?" and quickly mash Ctrl+F in a mad fit to find the variable's original source. No, instead, it was a Javascript abuse, more of a trick than a magic trick. Always declare your variables at the top of their scope. Just because you don't have to, doesn't mean you shouldn't.
function(){ var a, //description b; //description //process... }?
You're a brilliant programmer, you eat C++ code for lunch and poop Lisp. You know what variable scope is, you have full control over your variables and watch them like an overlord. Then Javascript put a metaphorical laxative in your coffee and had a good laugh.
var herp="one"; { var herp="two"; } alert(herp);?
The value of herp in this case is not one, it's two. Javascript variable scope is not dependent on blocks like other languages. Javascript variable scope is function based. Each function has its own scope, Javascript is far too cool to concern its self with meaningless curly braces. In fact, Javascript is so cool that it will let you pass scope around like any other namespace or variable.
Javascript, from the ground up, is an object oriented language. Everything in Javascript is an object, everything! Even literals like integers and strings are implicitly converted to objects with their built in constructors. The difference Javascript has when compared to other object oriented languages is that it lacks classes. Javascript objects are defined as functions, and even functions themselves are objects. Javascript has a property named prototype inherent in all objects that lets you mutate the st