It’s a good idea to declare the modules your code relies on at the top of your function or module. The declaration involves creating only a local variable and pointing to the desired module.

var myFunction = function() {

    // dependencies

    var event = YAHOO.util.Event, 
dom = YAHOO.util.Dom; // use event and dom variables // for the rest of the function... };

Benefits

• Explicit declaration of dependencies signals to the users of your code the specific script files that they need to make sure are included in the page.

• Upfront declaration at the top of the function makes it easy to find and resolve dependencies.

• Working with a local variable (such as dom) is always faster than working with a global (such as  YAHOO) and even faster than working with nested properties of a global variable (such as  YAHOO.util.Dom), resulting in better performance. When following  this  dependency  declaration  pattern,  the  global  symbol  resolution  is performed only once in the function. After that the local variable is used, which is much faster.

• Advanced minification tools such as YUICompressor and Google Closure compiler will rename local variables (so event will likely become just one character such as A), resulting in smaller code, but never global variables, because it’s not safe to do so.

 References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

最新文章

  1. Microsoft Loves Linux
  2. webapi filter过滤器中获得请求的方法详情(方法名,Attributes)
  3. mac boot2docker certs not valid with 1.7
  4. IIS7.5开启GZip压缩
  5. SQLServer------解决IP地址登录不了数据库问题
  6. OC面向对象特性: 继承
  7. linux初学 :简易的shell脚本
  8. arduino入门学习实现语音控制LED灯
  9. Git链接到自己的Github(1)简单的开始
  10. angularjs项目中关于服务的应用
  11. Visual Studio常用的快捷键
  12. android keytool 不是内部命令或外部命令在 (win7下不能用的解决方法)
  13. Linux-2.6.32内核编译流量计数器nfacct
  14. 嵌套json的查询
  15. HTTP协议缓存策略深入详解之ETAG妙用
  16. Struts2学习笔记(七)——类型转换
  17. [自制操作系统] 原子操作&核间中断&读写锁&PRWLock
  18. 201521123035《Java程序设计》第四周学习总结
  19. 《Master Bitcoin》学习笔记01
  20. Ajax传递List对象到前台展示问题遇到的坑

热门文章

  1. LeetCode3:Longest Substring Without Repeating Characters
  2. How to DEBUG a trigger or procedure
  3. 第 30 章 使用 Emmet 插件
  4. 简单理解——面向切面编程(AOP)
  5. 一次Debug过程的思考
  6. mysql 更改默认数据目录
  7. 网上图书商城2--Category模块
  8. Criteria查询初学者的采纳点哦
  9. JVM的生命周期、体系结构、内存管理和垃圾回收机制
  10. js 字符串 replace replaceAll