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