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.
1. You're not using a namespace.
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();
2. You're a magician, creating variables out of thin air.
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... }
3. You don't understand variable scope in Javascript.
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.
4. You think Javascript OOP is a tack on.
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 structure of objects and decorate them with more variables and functionality.
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 looks foreign to you, I'd like to introduce you to my good friend Google. Google is good at helping people learn about things. OOP is way too big of a topic for my short, comically condescending, numbered list. If you need help finding Google try using your favorite search engine.
5. You're using the new keyword like a blind guy with a cross bow.
Javascript must be your first girlfriend, because you have no idea how to use that thing. If you want to please Javascript like a real man you're going to need to learn about object literals. With the exception of instantiating objects, and some rare cases where you need lazy data loading, you probably shouldn't be using the new keyword. Allocating lots of new variables is slow in Javascript, and you'll always get better performance using object literals.
var rightway= [1, 2, 3]; var wrongway= new Array(1, 2, 3);
Remember all that talk about Javascript scope being function based? Remember someone mentioning that Javascript objects are defined as functions? Not using the new keyword when instantiating an object will blow your mind as it sets the scope of the object to the global namespace. It's good habit to always instantiate objects with new .
var derp="one"; var Herp=function() { this.derp="two"; } var foo=Herp(); alert(derp);
Javascript won't complain if you do this, and will actually alert the answer two! There are ways to write objects to prevent this behavior using instanceOf() but a better nine to five solution is to use the new keyword correctly like the professional that you are.
Now that you know why your Javascript code sucks, you can make it suck less by remembering these factoids. Of course, there's other reasons that your Javascript code sucks. I like three space tabs, I prefer underscores to camel case, and I like to capitalize my function names that represent classes. Of course, that's a discussion for another day. There's a lot of reasons why your Javascript code sucks, and probably just as many reasons why mine sucks, so feel free to share, add, agree, or tear me a new one in the comments.
From : http://patrickavella.com/blog/5-reasons-your-javascript-stinks
发表评论
-
Making preformated <pre> text wrap in CSS3, Mozilla, Opera and IE
2012-01-13 20:47 1253From: http://myy.helia.fi/~kart ... -
CSS cursor
2011-12-27 12:44 847浏览器支持 所有主流浏览器都支持 cursor 属性。 ... -
JavaScript for in语句
2011-12-09 15:56 759var tom = {name: 'Tom', gend ... -
JavaScript操作剪贴板 - ( for IE )
2011-12-06 12:13 2225bRet = [window.]clipboardData.c ... -
Javascript trim
2011-08-16 00:24 557In programming, trim is a str ...
相关推荐
本文是从 5 Reasons Your Javascript Stinks 这篇文章翻译而来。 Javascript在互联网上名声很臭,但你又很难再找到一个像它这样如此动态、如此被广泛使用、如此根植于我们的生活中的另外一种语言。它的低学习门槛...
42 Reasons To Start a Business Analyst Career
在IT行业的电源设计领域,电源不稳定性是一个常见且复杂的问题,它可能由多种原因引起,对设备性能和可靠性造成严重影响。根据给定的文件信息,我们可以深入探讨导致电源不稳定的六大常见原因,以及如何识别和解决...
Dedicated implementations of these algorithms and data structures are much better for performance and security reasons. We also do not provide any guarantee for api stability. Before contributing to
标题中的"Python库 | reasons-0.1.0-py3.8.egg"表明这是一个针对Python编程语言的库,具体来说是名为“reasons”的库,版本为0.1.0,它针对的是Python 3.8环境。在Python生态系统中,库是开发者常用的工具,用于提供...
This book challenges, with several powerful arguments, some of our deepest beliefs about rationality, morality, and personal identity. The author claims that we have a false view of our own nature;...
engine-move_reasons.c
engine-move_reasons.h
What Microsoft Azure can do (infrastructure and platform as a service), and reasons for adoption How to plan for successful integration of Azure with your existing IT infrastructure How to use the ...
5. **智能调度技术**:利用智能调度功能,安全任务会自动在计算机处于空闲状态时执行,避免在工作高峰期对用户造成干扰。这样,安全更新和扫描等非关键任务可以在不影响正常工作的前提下完成。 Symantec Insight...
家里台式机和笔记本安装的KEIL与Jink版本相同,台式机一直使用正常,这几天准备用笔记本使用KEIL在线仿真,选择Jlink时Keil自动闪退,或报“Cannot load driver 'C:\Keil_v5\ARM\Segger\JL2CM3.dll ”,更新...
资源来自pypi官网。 资源全名:reasons-0.1.0-py3.8.egg
5. **硬件故障**:硬盘错误、内存条故障或电源问题等硬件故障也可能导致崩溃。定期进行硬件检查和维护,使用可靠硬件,有助于预防这类问题。 6. **系统文件损坏**:关键的系统文件丢失或损坏会导致蓝屏死亡(BSOD)...
在 "SF_Top10_Reasons - V2.pptx" 文件中,提到了升级到 Storage Foundation 或 VxFusion(可能的最新版本)的十大理由。以下是对这些关键特性和优势的详细解释: 1. **异构平台支持**:Storage Foundation 支持...
多语言支持控件 Five reasons why would you need TsiLang Components Suite: - You need to localize or internationalize your applications - You don‘t want to spend a lot of time for translating your ...
【高通QMI WDS Reasons 错误码详解】 在无线通信领域,特别是涉及到5G技术的设备中,高通的QMI(Qualcomm Message Interface)协议是用于控制和管理移动设备与网络之间通信的重要接口。QMI WDS(Wireless Data ...