5 Reasons Your Javascript Stinks

本文探讨了JavaScript代码质量低下的五个主要原因:未使用命名空间、滥用全局变量、不理解变量作用域、错误地理解面向对象编程以及不当使用new关键字。通过理解这些原因,开发者可以改进代码质量。

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

内容概要:本文档是一份针对2025-2026年Java后端大厂面试的高频考点全面梳理,涵盖Java基础、集合框架、并发编程、JVM、Spring全家桶、MySQL、Redis、消息队列、分布式与微服务等核心技术模块。内容不仅包括经典概念辨析(如String与StringBuilder区别、HashMap底层结构),还深入源码机制与设计原理(如Spring三级缓存解决循环依赖、AOP动态代理实现),并结合实际场景探讨问题排查与技术选型(如GC调优、缓存穿透解决方案)。特别强调从“背八股”向源码理解、线上排障和设计权衡的能力转变,体现当前面试趋势的深度化与实战化。; 适合人群:具备1-3年工作经验,准备冲击中高级Java岗位的研发人员,尤其适合希望系统提升面试竞争力、深入理解主流技术底层原理的开发者。; 使用场景及目标:①应对大厂Java后端技术面试,掌握高频考点与最新趋势;②深入理解核心技术的设计动机与实现细节,如ConcurrentHashMap的线程安全机制、分布式ID生成方案对比;③提升实际问题分析与解决能力,如Full GC排查、事务失效定位等。; 阅读建议:此资源以面试为导向,兼具广度与深度,建议结合自身项目经验进行对照学习,注重理解“为什么”而非仅仅记忆结论,对关键知识点应动手验证(如ThreadLocal内存泄漏实验),并在模拟面试中强化表达逻辑。
代码下载链接: https://pan.quark.cn/s/8df2b016201b 555 芯片的引脚布局、功能特性、引脚示意图以及引脚说明是关键信息。555 芯片作为一种集成电路,具有多样化的功能特性,在定时器、定时延时控制、调光、调温、调压、调速等多种控制及计量检测领域有着广泛的应用。接下来将展示 555 芯片的引脚示意图和引脚说明: 1. 555 芯片引脚示意图:555 芯片包含 8 个引脚,具体如下: * 1 脚:地线端 * 2 脚:触发输入端 * 3 脚:输出端 * 4 脚:复位端 * 5 脚:控制端 * 6 脚:阈值端 * 7 脚:放电端 * 8 脚:电源端 2. 555 芯片引脚说明: * 1 脚:地线端,用于连接电路的负极部分。 * 2 脚:触发输入端,用于接收外部信号的输入,进而控制输出端的状态。 * 3 脚:输出端,输出高电平或低电平信号,其状态受触发器控制。 * 4 脚:复位端,当输入低电平时,输出端会输出低电平信号。 * 5 脚:控制端,用于调节输出端的状态,能够改变上下触发电平的数值。 * 6 脚:阈值端,作为上比较器的输入端,当输入高电平时,输出端会输出低电平信号。 * 7 脚:放电端,是内部放电管的输出端,其输出电平状态受触发器控制。 * 8 脚:电源端,用于连接电源的正极部分。 3. 555 芯片工作原理:555 芯片的工作原理是通过上比较器和下比较器来控制输出端的状态。上比较器的输入端位于 6 脚,而下比较器的输入端位于 2 脚。根据输入端的电平状态,输出端会输出高电平或低电平信号。 4. 555 芯片应用领域:555 芯片在各种电子产品中有着广泛的应用,例如在定时器、定时延时控制、调光、调温、调压、调速等领域。它还可以用于...
内容概要:本文提出了一种基于遗传算法的微电网调度优化方案,针对包含风能、太阳能、蓄电池和微型燃气轮机等多种分布式能源的微电网系统,构建了综合考虑经济性与稳定性的多目标优化调度模型。通过Matlab编程实现遗传算法求解,对微电网内部各单元的出力进行合理分配与协调控制,以实现运行成本最小化、可再生能源利用率最大化以及系统功率平衡和稳定性提升。文中详细阐述了系统建模过程、遗传算法的编码方式、适应度函数设计、约束条件处理机制及仿真结果分析,充分验证了该方法在降低综合运行成本、提高能源利用效率和增强系统调度灵活性方面的有效性与实用性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事新能源、微电网优化调度相关研究的研究生及科研人员。; 使用场景及目标:①学习并掌握遗传算法在微电网多能源协调调度中的建模与应用方法;②复现、改进或拓展微电网经济调度模型;③为实际微电网能量管理系统的设计提供算法支持与仿真验证依据。; 阅读建议:建议读者结合提供的Matlab代码,深入理解遗传算法的种群初始化、交叉变异操作、适应度评估及收敛判断等关键环节,并可通过调整能源配置参数、负荷需求或引入新的约束条件(如碳排放、设备寿命)进行拓展研究,以深化对智能优化算法在综合能源系统中应用的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值