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的更多相关文章

  1. JavaScript Patterns 5.4 Module Pattern

    MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...

  2. 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 ...

  3. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  4. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  5. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  6. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  7. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  8. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  9. 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 ...

随机推荐

  1. C# ~ 数据库连接

    C#连接数据库(SQL Server)进行数据的读取和写入,学习之. 1. 数据库连接 服务器:“Data Source=服务器IP; Initial Catalog=数据库; User ID=用户名 ...

  2. iOS 阶段学习第22天笔记(JSON数据格式介绍)

    iOS学习(OC语言)知识点整理 一.JSON数据格式 1)概念:json是一种网络数据传输格式,有值/对象:{“A”:1,”B”:”2”…}词典:对象的序列:[,,,,,]数组两种数据类型 2)UR ...

  3. 【C#】第3章学习要点(二)自定义类和结构

    分类:C#.VS2015 创建日期:2016-06-19 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.要点概述 别人提供的类都是为了简化你的工作量用的,可是 ...

  4. java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX

    出现这个问题的解决方案就是将原有的jar删除  然后重新下载过一遍就可以使用了  我估计是元数据等损坏了

  5. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  6. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

  7. Git基本使用教程

    1.创建版本库      版本库又可以称为仓库(repository),可以简单理解为一个目录,在这个目录下的所有文件都可以被git管理起来,每个文件的新增.修改.删除Git都可以跟踪,以便在任何时刻 ...

  8. 高效 Java Web 开发框架 JessMA v3.2.3 正式发布

    JessMA(原名:Portal-Basic)是一套功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hib ...

  9. IO流(四)__流的操作规律总结

    流的操作规律: 1.明确源和目的 源:InputStream Reader 目的:OutputStream Writer 2.明确数据是否是纯文本数据 源:是纯文本:Reader 否:InputStr ...

  10. java中newInstance()和new()

    在Java开发特别是数据库开发中,经常会用到Class.forName( )这个方法.通过查询Java Documentation我们会发现使用Class.forName( )静态方法的目的是为了动态 ...