JS代码的简单重构与优化(适合新手)
Demo . 1
//bad
if (age > 20) {
return true;
} else {
return false;
} //good
return age > 20;
这种一看就明白吧,没什么说的。
Demo . 2
//bad
for (var i = 0; i < arr.length; i++) {
//do something...
} //good
for (var i = 0, len = arr.length; i < len; i++) {
//do something...
}
将数组的长度进行存储,循环时就不用每次去读取一次数组长度,从而提升性能
Demo . 3
//bad
if (value !== "") {
//do something...
} //good
if (value) {
//do something...
}
js中所有 非空字符 作布尔运算时,都会返回 true,反之返回 false。 (空格 属于非空字符)
Demo . 4
//bad
if (value !== 0) {
//do something...
} //good
if (value) {
//do something...
}
js中所有 非零 数字作布尔运算时,都会返回 true, 反之返回 false。
Demo . 5
//bad
if (user.id === 10) {
if (user.name !== "") {
if (user.email === "email") {
//do something...
}
}
} //good
if(user.id === 10 && user.name !=="" && user.email === "email") {
//do something...
} //good
if (user.id !== 10) return;
if (user.name === "") return;
if (user.email !== "email") return;
//do something...
多层条件嵌套,进行 转换 或 拆分。
Demo . 6
//bad
var a = "aa";
var b = "bb";
var c = "cc";
var d; //good
var a = "aa",
b = "bb",
c = "cc",
d ;
多个变量的定义,写一个 var 即可,遵循的原则:被赋值的在前面,只是进行声明放后面(d 在最后)
Demo . 7
//bad
for (var i = 0; i < 100; i++){
str += str;
document.getElementById("box").innerHtml = str;
} //good
for (var i = 0; i < 100; i++) {
str += str;
}
document.getElementById("box").innerHtml = str;
尽可能的减少DOM操作,将DOM操作放在循环体之外,数据处理好后,只执行一次DOM操作即可。
Demo . 8
//bad
function test() {
var a = b = 1;
console.log(a);
} //good
function test() {
var a = 1,
b = 1; console.log(a);
}
避免使用 连等号 声明赋值变量。js基础好点的同学都知道,这里存在着一个坑: b 会被声明为全局变量,意思就是在 test() 方法之外b也是可以被访问到的。 全局变量是不推荐使用的,容易污染环境。
Demo . 9
//bad
if (age > 20) {
console.log("年龄大于20");
} //good
(age > 20) && console.log("年龄大于20");
这里的bad并不是说第一种写法不好,其实就可读性来说,第一种比第二种更好。 只是第二种写法更加优雅一点(个人觉得)。 这个句法可能有些同学用的比较少,解释一下:
如果第一个条件为 true ,那么就执行后面的语句,第一个条件为false, 后面的语句便不会执行。后面的语句可以是表达式,方法,或变量,常量都可以。 比如 (age > 20) && test()。用于赋值时的具体说明,如下图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfEAAAETCAIAAACp3KijAAAeRElEQVR4nO3dPYzc5p3H8W0OyTWBmqQ8BCqmcOJOKa4VdEgMTOBD3CwQWIEXiJHmitjrRIWQTkUgGGlOhZKDJs1A2cJptkmQwuAK8o6kHXBXM5FAwAvDoAHDFQU3BtLwCpIPn4d8Hr7M8OUh5/uBgHg5M3zLzG+e+ZP8c+/y8jIEAIzCXu1M991T71U7KwMA2ErdTA+8hetnpvlubhIAoAc1Mz3wTvP5TaYDgB3qZbrvnnpB7s8o0wPPlR8DAHSuTqZrBun+uXPixM4ZrANAv2pkuq6UHgbe4sna905PKL8AQO+qZ3qm7hL7zP80evTi+edNrhcAoL6qma4dpAMArFIx04l0ABiAapnOdUYAMARVMp1BOgAMQ/3eAAAAW5HpADAeTWR6o70B9KdMtibwFo7jOBdjqy3F2+U4JwvNlb8AxsqyTA88NYNa5rvOhT/Crga+K3Zj4C3kbyyOjQDjtlWmN97vpZ/Eqbfmoh1CpheCaXoPvvYupA0KvGf/lP/q9FsTQLe2HKc32+8l8E5P1LxJ5y8lUTzx1Hsl/tv1Q991HGfxmf9pXHRw0hdEhYhT71VSkVBTrU6mm751an4bdbJd+q3L72QA47Ft7aXJfi+Bl/liUKq/UZ0k+ePciYLJP3fSRfuu/NWivCDOv3iC78uLqTVONz25zkw62q78k5NJlF+Asdo205vs95ItC8g/AvI/Bfxz52TxOHMEUE0rudBfkLn14lg/HDZN186js+3KzEfMjSvIgLGy6VzG7Di98CSNKEQfL+R4yr6g8UyPr6f13VPPXz9NR8Cm6ea5tL5dvmuqhjFOB0bMpkzPlXqN6SOd1yGPOn1XOXXPXz+VaxSNZHqc19IKFE03z6b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l+zJkiXNYeQXtdLvRUzX3GO67FFOpgEGjX4vDfR70c+9ZAVb7PciHjBnetqMV54P9XRg6Oj3kl9u3X4v+rnLM+i430vMlOmBt3hsvJM09XRg0Oj3kl9u3X4vJWvVQ7+X5DFNpkuHQ40v09bZAQwB/V40y63b70U/96Int93vJX7QzZ2vKJ3CmGa376a3uFPOhwEwNPR7KdmuKv1eio6R9tHvJW32opyKHninJ7rp0Zw0EwEMzhDOZewX/V4ADAeZXoJ+LwAGhEzXot8LgEGyO9OH1e9l42fVUKcOI2rkjlNSo0kK8HwrAENnd6ZH7OsNsH09fUM1LiWtX21npA8MH5m+iRpJ3Vu/l/q3libTgeGj34t2PvX6vRQ+v4d+L5lzGdPzzaUHNPGty3RRv/nk7IX0JeGfOyec+whYiH4vDfR7KX5+T/1edOP0wPdFKru5Ins+06Up8vVKyqw1nWMA9IZ+L/nl1u33UvL8nvq9aDNdGcCXZ7pymZLoKJAO0nMPAegZ/V7yy63b76Xk+T31e8lnujKi1rywuJ4eeMk4vX6lHkBX6PeiWW7dfi+Fz++r34su09WCeGmmK7NIM11XtwFgB/q9lGxXlX4vRc+3oN+LGJ1L0xcvzx4l5ZdsHxhRS5FOcM8UWJSXcJgUsMcQzmXsV91+L+rz6fcCoEtkeom6/V7U59PvBUCnyHStuv1eLO4PA2CXkOkAMB5kOgCMB5kOAONBpgPAeJDpADAeZDoAjAeZDgDjQaYDwHiQ6QAwHv8PjlyQ+Hb6iAQAAAAASUVORK5CYII=" alt="" />
Demo . 10
//bad
if (age > 20) {
value = "similar";
//do something...
} //good
if (age > 20 && (value = "similar")) {
//do something...
}
这个可以说是Demo9的变体,代码可读性还是第一个比较好,当你采用第二种写法时,请配合注释使用。因为有些人可能会认为你这是书写失误,少写了一个 = ,从而他自行给你补上了(value == "similar")。而你实际上是想做赋值操作的。因此你要写上注释说明,你这里不是书写失误,而是要进行赋值。
注: 根据博友留言的帮助,此处应用需小心,当你为value赋值为空串 或 0 或 null 等等(也就是说,value作布尔运算返回值为 false)的时候,整个判断表达式会返回 false, 也就不会执行if花括号里面的代码程序。所以当你不确定 value 会被赋值为什么的时候,建议不要这样用。
Demo . 11
//bad
if (value === "") {
value = "similar";
} //good
if(!value) {
value = "similar";
} //good
value = value || "similar";
这个和Demo9的 && 号运算正好相反,当第一个条件为 false 时,执行后面的语句。因为空字符串作布尔运算是返回 false的,所以可以这样用。
Demo . 12
//bad
i = i + 1;
i = i - 1; //good
i++;
i--;
这个没什么好说的。
Demo . 13
//bad
if (typeof str === "String") { } //good
if (typeof str === "string") { }
这里只是帮大家填个坑,不存在好坏。typeof 运算是用来检查数据类型的(常用来检查js中的基础类型),它返回的是一个字符串(并且始终为小写),所以第一个始终为false。 检查js中的引用类型要用 instanceof 运算, 如: var arr = []; if(arr instanceof Array){}, 但 Function 类型比较特别,也可以用 typeof 来进行判断。
Demo . 14
//(bad) 格式化字符串 fontSize => font-size
function stringFormat(str) {
var strArr = str.split(''),
len = strArr.length,
i = 0; for (; i < len; i++) {
if(/^[A-Z]$/.test(strArr[i])) {
strArr[i] = "-" + strArr[i].toLowerCase();
}
} return strArr.join('');
} //(good) 格式化字符串 fontSize => font-size
function stringFormat(str) {
return (str.replace(/([A-Z])/g, "-$1")).toLowerCase();
}
两个方法实现同样的功能,但第二个优雅得多,性能上也比第一个要好。当对字符串进行循环操作,如:查找、替换。 首先考虑的应该是使用 正则,不应该是循环遍历。
Demo . 15
//bad
function regist(userName, userPwd, userEmail, userPhone) {
//do something...
} //good
function regist(user) {
//do something
}
当一个方法接收的参数过多的时候,可以考虑将其封装成一个json对象, 这样简单得多。
Demo . 16
var a = 1,
b = "1"; //(bad) 输出 a等于b
if (a == b) {
console.log("a等于b");
} //(good) 输出 a不等于b
if (a === b) {
console.log("a等于b");
} else {
console.log("a不等于b");
}
推荐使用 全等 判断,这样更准确。 字符串和数字并不是全等的。关于比较的一点说明:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnYAAADHCAIAAAAbP4lXAAAgA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CpPrH+Lq7A5iXWqRTh97ddx2rJnpO7NmeAoBgD8G5z7XizbfPR8Mo8j9SEfkFga2ba1pvtarJP5QxH4q7kh077CsrmLrjqrWshNwh9O6qgqewoarVrJbYgHAAC+Hnx64uN53K7FZdPvTQ4AAH4XSOynA4UFAIAvBRILAAAAdAESCwAAAHQBEgsAAAB04X/W0lDXVOJBgAAAAABJRU5ErkJggg==" alt="" />
Demo . 17
//bad
if (age > 20) {
value = "aa";
} else {
value = "bb";
} //good
value = age > 20 ? "aa" : "bb";
目前只想到这么多,麻烦大家进行补充,感谢!
JS代码的简单重构与优化(适合新手)的更多相关文章
- JS代码的简单重构与优化
JS代码的简单重构与优化(适合新手) 原文 http://www.cnblogs.com/similar/p/5016424.html Demo . 1 //bad if (age > 20) ...
- CSS代码重构与优化之路(转)
CSS代码重构与优化之路 阅读目录 CSS代码重构的目的 CSS代码重构的基本方法 CSS方法论 我自己总结的方法 写CSS的同学们往往会体会到,随着项目规模的增加,项目中的CSS代码也会越来越多 ...
- JavaScript进阶之路——认识和使用Promise,重构你的Js代码
一转眼,这2015年上半年就过去了,差不多一个月没有写博客了,"罪过罪过"啊~~.进入了七月份,也就意味着我们上半年苦逼的单身生活结束了,从此刻起,我们要打起十二分的精神,开始下半 ...
- [JQuery]用InsertAfter实现图片走马灯展示效果2——js代码重构
写在前面 前面写过一篇文章<[JQuery]用InsertAfter实现图片走马灯展示效果>,自从写过那样的也算是使用面向对象的写法吧,代码实在丑陋,自从写过那样的代码,就是自己的一块心病 ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】
原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!
原文地址 迁移到:http://www.bdata-cap.com/newsinfo/1741515.html 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和f ...
- JS一般般的网页重构可以使用Node.js做些什么(转)
一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...
- JavaScript是如何工作的: CSS 和 JS 动画底层原理及如何优化它们的性能
摘要: 理解浏览器渲染. 原文:JavaScript是如何工作的: CSS 和 JS 动画底层原理及如何优化它们的性能 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 这是专门探索 J ...
- vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结
vue—你必须知道的 目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...
随机推荐
- lintcode: 翻转链表
题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...
- iOS LLDB调试器
随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...
- Shuffle和排序
MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...
- asp.net Twilio
Imports System.Net Imports System.Text Imports Twilio Public Class clsTwilioSMS Public Shared Functi ...
- kail新手安装
Kali Linux下载链接:http://mirrors.ustc.edu.cn/kali-images/kali-2.0/ (ps:建议用国内镜像源下载,速度有保证) Kali Linux安装之后 ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
- mac下装Ruby
https://ruby-china.org/wiki/install_ruby_guide
- HBase学习笔记
关键类: HBaseAdmin 管理Hbase的,主要负责DDL操作 HTable 管理表中数据,主要负责DML操作 1.为了避免热点,更多的建表方法 在Shell中: },{SPLITS=>[ ...
- 【设计模式】—— 单例模式Singleton
前言:[模式总览]——————————by xingoo 模式意图 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有公开的调用方法. 模式结构 ...
- ha_innobase::open
http://mysql.taobao.org/monthly/2015/08/07/ /******************************************************* ...