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)
JavaScript Patterns 5.2 Declaring Dependencies的更多相关文章
- JavaScript Patterns 5.4 Module Pattern
MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns
In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...
随机推荐
- C#编程总结(七)数据加密——附源码
C#编程总结(七)数据加密——附源码 概述 数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”,使其只能在输入相应的密钥之后才能显示出本来内容 ...
- c#如何采集需要登录的页面
首先说明:代码片段是从网络获取,然后自己修改.我想好的东西应该拿来分享. 先说下原理:当我们采集页面的时候,如果被采集的网站需要登录才能采集.不管是基于Cookie还是基于Session,我们都会首先 ...
- hibernate------java-delete-insert-update
**************************************************************************************************** ...
- flask-cors
https://github.com/corydolphin/flask-cors/blob/master/examples/app_based_example.py ""&quo ...
- web项目 验证码 *** 最爱那水货
1. jsp代码 : <Script> function changeImg(){ document.getElementById("certImg").src =&q ...
- Android开发跳槽、简历和面试的那些事
年后不久,就迎来了一年一度的招聘旺季,尤其,对于互联网行业来说,近些年的3月份被视为换工作的最高峰,已经没什么可以争议的了. 至今为止,在小组Android开发招聘这块,已经面试有近30人了.最后得出 ...
- java注释规范
前言: 现在java的出产地sun公司并没有定义一个java注释规范,注释规范目前是每个公司自己有自己的一套规范,主要是为了团队之间的协作. 1.基本规则 1.注释应该使代码更加清 ...
- Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...
- Lua-泛型for循环 pairs和ipairs的区别
先看一段简单的代码: local mytable = { , , aa = "abc", subtable = {}, , } --for循环1 print("for - ...
- js快速排序方法
function quickSort(arr){ if(arr.length<=1){ return arr; } var arrIndex=Math.floor(arr.length/2); ...