LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

js基础:JavaScript的五个技巧

admin
2010年8月18日 10:21 本文热度 3121
五条javascript技巧帮助你提高代码质量:
 1.只在<form>元素上使用submit事件
  绑定事件处理表单时,使用表单得submit事件而不是click事件。
 2.如果可点击,确认是否是链接
 3.简单的for循环优化
  下面是对for循环做的一个非常简单的改变,但是它确可以提高循环的效率
 
 
 for ( var i = 0; i < elements.length; ++i )
 for ( var i = 0, j = elements.length; i < j; ++i )

   第二行代码中elements.length被存在变量j中,所以不必在每次循环的时候重复读取它。
 
 4.事件处理时使用匿名函数(Use anonymous functions for event handlers)
  象下面这样比较短的函数使用匿名函数比引用一个其他地方命名的函数可读性要高
 
anchor.onclick = function() {
                    map.goToPosition( home );
                    return false;
                 }

 5.使用Array.join代替字符串连接(concatenating strings)
  遇到比较长的字符串连接的时使用Array.join代替字符串连接可以获得更好的效率而且可读性更高。
 
 
 var text = 'There are' + elements.length + 'members in the       
         elements array.';
                var text = ['There are', elements.length, 'members in the
elements array.'].join(' ');

 注:关于最后一条好像还有争议
 
On 12 Sep 07
Stuart Colville said:
I'd disagree with the last item on using Array.join for concatenating strings: Firstly, the only real benefit in using this approach is when you are concatenating a lot of strings together. With three strings it may also actually be slower due to the overhead of initialising the array.
Secondly the performance increase from using Array.join in the right situation is generally only seen in IE due to it's slow JavaScript engine which has not seen the same levels of optimisations that have been made in other browsers.
 
 
Update
A number of people have mentioned to me that the Array.join technique for string concatenation is a bad one, particularly if you're only doing it with a small number of strings. Our benchmarks show it being faster for IE when you get to about 6/7 string concatenations, so it's been useful to us in some situations. But I'd agree with Stuart below that for the average situation it's not going to be worth it. However, I don't see using Array.join() for string concatenation as an abuse of JavaScript.

该文章在 2010/8/18 10:21:51 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved