24 javascript best practices for beginner(only 23 finally)
原文是英文,链接: http://net.tutsplus.com/tutorials/JavaScript-ajax/24-JavaScript-best-practices-for-beginners/
这篇是jquery的:http://yanhaijing.com/jquery/2013/12/05/%E9%AB%98%E6%95%88jQuery%E7%9A%84%E5%A5%A5%E7%A7%98/
我自己在学习,算是自己为自己整理的笔记。
1:用===代替==(比较符)
如果两个操作数具有相同的类型和值,则===返回true,!==返回false。
===是严格等于,==是等于。
前者:比较类型和数值,若类型不等则比较结果一定不等。
后者:比较数值,若类型不等,则比较结果可能相等。因为比较时若类型不等,则会强制转换类型,然后比较。
2:eval(作者不推荐用,因为带来方便外还带来了巨大风险)
看代码吧.(eval函数接收一个参数s,如果s不是字符串,则直接返回s。否则执行s语句。如果s语句执行结果是一个值,则返回此值,否则返回undefined。)
var code1='"a" + 2'; //表达式
varcode2='{a:2}'; //语句
alert(eval(code1)); //->'a2'
alert(eval(code2)); //->undefined
alert(eval('(' + code2 + ')')); //->[object Object]
3:尽量不省略花括号或者括号。虽然有时候浏览器能正常编译,但是隐患很大。
如果非要省,尽量在只有一行代码的时候省略,不过还是不建议,因为在将来你很有可能在该if语句中添加语句!
4:使用js工具debug(见英文原文如下)
JSLint is a debugger written by Douglas Crockford. Simply paste in your script, and it’ll quickly scan for any noticeable issues and errors in your code
5:把js放在页面的前面,如:
<p>And now you know my favorite kinds of corn. </p>
<script type="text/javascript" src="path/to/file.js"></script>
<script type="text/javascript" src="path/to/anotherFile.js"></script>
</body>
</html>
6:在for语句的外面定义变量
7:尽量用像join这样的本地方法
var arr = ['item 1', 'item 2', 'item 3', ...];
var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>';
大神的话:
I won’t bore you with benchmarks; you’ll just have to believe me (or test for yourself) - this is by far the fastest method!
Using native methods (like join()), regardless of what’s going on behind the abstraction layer, is usually much faster than any non-native alternative.
- James Padolsey, james.padolsey.com
8:减少全局变量
9:注释你的代码
10:不要想当然你的代码阅读者会有运行js的环境,很有可能被禁止了。
11:不要传递一个Stringt给 "SetInterval" or "SetTimeOut"方法
12:不要使用with语句,如下3种,推荐度:3>1>2
with (being.person.man.bodyparts) { //第一种
arms = true;
legs = true;
}
being.person.man.bodyparts.arms = true; //第二种
being.person.man.bodyparts.legs= true;
var o = being.person.man.bodyparts; //第三种
o.arms = true;
o.legs = true;
13:用{}代替new Object{}
14:用[]代替new Array{}
15:定义多个变量时,省略var关键字,用逗号连接并列的变量,如下:
var someItem = 'some string';
var anotherItem = 'another string';
var oneMoreItem = 'one more string';
var someItem = 'some string',
anotherItem = 'another string',
oneMoreItem = 'one more string';
17:一定要时刻记得用分号,尽管有时候浏览器会允许你省略分号。
18:“For in”语句
for(key in object) {
if(object.hasOwnProperty(key) {
...then do something...
}
}
19:用firebug的timer优化代码
20:review,读,再读,再读你的代码
21:初始化函数
22:原生态js比使用库要运行的快
23:引用大神的一个json分析器
Although JavaScript 2 should have a built-in JSON parser, as of this writing, we still need to implement our own. Douglas Crockford, the creator of JSON, has already created a parser that you can use. It can be downloaded HERE..
Simply by importing the script, you'll gain access to a new JSON global object, which can then be used to parse your .json file.
view plaincopy to clipboardprint?
var response = JSON.parse(xhr.responseText); var container = document.getElementById('container');
for(var i = 0, len = response.length; i < len; i++) {
container.innerHTML += '<li>' + response[i].name + ' : ' + response[i].email + '</li>';
}
24:移除language属性
<script type="text/javascript" language="javascript">
...
</script>
that's all,folks
---------------------------分割线------------------------------
以下是在原文的评论中看到的。
1:减少使用无名函数或者回调函数的数量
2:使用指定事件处理程序
$('#element1').bind('click', function(event){
$('#element2').bind('click', function(event){
/* do something */
});
}
);
24 javascript best practices for beginner(only 23 finally)的更多相关文章
- JavaScript Best Practices (w3cschool)
JavaScript Best Practices (w3cschool) Local Variables: · 总是在前面集中定义变量,(包括 for 的i).(strict mode) ...
- JavaScript best practices JS最佳实践
JavaScript best practices JS最佳实践 0 简介 最佳实践起初比较棘手,但最终会让你发现这是非常明智之举. 1.合理命名方法及变量名,简洁且可读 var someItem = ...
- JavaScript练习笔记整理·1 - 6.23
练习平台Codewars地址:https://www.codewars.com/ 欢迎和大家一起来讨论~╭( ・ㅂ・)و ̑̑ 基础练习(1): 我的解答为: class SmallestIn ...
- 24 JavaScript对象访问器&JavaScript对象构造器
ES5引入了Getter和Setter Getter和Setter允许定义对象访问器 JavaScript Getter(get关键字):获取对象属性 <script> var perso ...
- Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and the ...
- Javascript高级编程学习笔记(23)—— 函数表达式(1)递归
前面的文章中,我在介绍JS中引用类型的时候提过,JS中函数有两种定义方式 第一种是声明函数,即使用function关键字来声明 第二种就是使用函数表达式,将函数以表达式的形式赋值给一个变量,这个变量就 ...
- JavaScript Best Practices
原文: https://www.w3schools.com/js/js_best_practices.asp --------------------------------------------- ...
- 给JavaScript初学者的24条最佳实践(转:http://www.cnblogs.com/yanhaijing/p/3465237.html)
作为“30 HTML和CSS最佳实践”的后续,本周,我们将回顾JavaScript的知识 !如果你看完了下面的内容,请务必让我们知道你掌握的小技巧! 1.使用 === 代替 == JavaScript ...
- JavaScript初学者应知的24条最佳实践(译)
原文:24 JavaScript Best Practices for Beginners 译者:youngsterxyf (注:阅读原文的时候没有注意发布日期,觉得不错就翻译了,翻译到JSON.pa ...
随机推荐
- Python中的@property装饰器
要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...
- Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Hybrid端口)
>Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Hybrid端口) >>实验开始,先上拓扑图参考: >>>实验目标:分别实现主 ...
- 洛谷 P3258 BZOJ 3631 [JLOI2014]松鼠的新家
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- sencha touch 2制作滑动DataView(无缝list)
Ext.define('App.view.Sections', { extend: 'Ext.dataview.DataView', xtype: 'sectionslist', id: 'mainl ...
- ZooKeeper可以用来做什么(转)
在ZooKeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- jQuery和CSS3炫酷button点击波特效
这是一款效果很炫酷的jQuery和CSS3炫酷button点击波特效.该特效当用户在菜单button上点击的时候.从鼠标点击的点開始,会有一道光波以改点为原点向外辐射的动画效果,很绚丽. 在线演示:h ...
- QT中使用高速排序
今天想到了用QT做一个高速排序.所以研究了一下. 由于用习惯了,C++的std::sort.就算是C的时候也用得是stdlib.h中的qsort. 手写板 手写板的快排事实上不难,仅仅是自从用C++打 ...
- linux下华为HSPA模块MU609的驱动问题
环境: CPU: s3c2416 Linux: 3.6 模块: HUAWEI MU609 SIM卡: 移动3G卡.移动4G卡 首先,拿到MU609模块后,第一要做的是对模块进行一些熟悉与了解,那么资料 ...
- C# winform窗体在桌面右下角显示(任务栏上方)
问题描述: 有一个主窗口程序,需要给该程序添加一个通知子窗口.子窗口的位置为右下角. 解决方法: 在子窗口frmPopMsg的代码文件中添加如下代码: public frmPopMsg() { Ini ...
- Base Class Doesn't Contain Parameterless Constructor?
http://stackoverflow.com/questions/7689742/base-class-doesnt-contain-parameterless-constructor #regi ...