[读书笔记]高性能JS-编程实践
2015-11-11 21:37
549 查看
This part is some example to practice.1 avoid double evaluationless use of eval,new Function,settimeout,setinterval.string in these method are executable js code.when use these method,js will execute code twice ,first is normal js,second is js code in string ,such as var a=eval('1+2');first Asignment,second run code in string '1+2';in normal,don't use new Function. try to use function instead of string both in serttimeout and setinterval;2 use literal;var obj={ 'a':1, "b":2}var arr=[1,2,3,4]these two are good and run fast than something asvar obj={};obj.a=1;obj.b=2;3 no repeated jobnormally, we addevent use the function as
function addEvent(obj,type,handler){ if(obj.addEventListener){ obj.addEventListener(type,handler,false) }else{ obj.attachEvent('on'+type,handler); } } function removeEvent(obj,type,handler){ if(obj.removeEventListener){ obj.removeEventListener(type,handler,false) }else{ obj.detachEvent('on'+type,handler); } } when called ,function will do repeated job for check which browser we using. actually ,one test is enough. one way to avoid is lazy load.
function addEvent(obj,type,handler){ if(obj.addEventListener){ addEvent= function (obj,type,handler) { obj.addEventListener(type,handler,false) } }else{ addEvent= function (obj,type,handler) { obj.attachEvent('on'+type,handler); } } addEvent(obj,type,handler) } call this method once,and new addEvent will override the past one.so next time called,it save lots of time. and another way is conditional load.
var addevent=document.body.addEventListener? function (obj,type,handler) { obj.addEventListener(type,handler,false) }: function (obj,type,handler){ obj.attachEvent('on'+type,handler); } cool way.
相关文章推荐
- js中innerText,innerHTML的用法
- ie6下js更新元素display:block后,仍然不显示的hack办法
- JavaScript事件列表
- 你不知道的JavaScript--Item32 DOM基础详解2
- JSON.parse()和JSON.stringify()
- Perl中调用路径接口(返回值为json)发送邮件
- JavaScript基础——引用类型
- Javascript实现图片的预加载的完整实现
- js实现打开网页设备判断
- jstack分析cpu占用100%
- js实现左右点击图片层叠滚动特效
- js获取网页或窗口的高宽
- js 如何获取自定义属性的值
- 面向对象的 JavaScript
- Javascript模块化编程(三):require.js的用法
- [LeetCode][JavaScript]Range Sum Query - Immutable
- JavaScript-获取浏览器信息
- (转)理解Javascript_01_理解内存分配
- (转)理解Javascript_02_理解undefined和null
- JSP内置对象---总结