- 浏览: 1397018 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (328)
- JSF (27)
- 生活 (12)
- Ajax (26)
- Maven (6)
- CSS (1)
- Shale (3)
- SiteMesh (1)
- Ext (15)
- JMX (2)
- Windows技巧 (7)
- 工作感悟 (18)
- SVN (2)
- SVG (0)
- GoogleGear (0)
- RAP (2)
- SOA与WebService (3)
- 笔记本技术研究 (1)
- Microsoft (2)
- 英语学习 (3)
- PHP (7)
- web 2.0 (6)
- 语义Web (1)
- IT史话 (3)
- iText (3)
- JVM (1)
- PropertiesEditor (1)
- J2SE (33)
- Spring (2)
- Java Batch (1)
- log (2)
- Struts2 (2)
- DWR (0)
- JAAS (3)
- EJB3 (4)
- Flex (8)
- JFreeChart (1)
- WAS (0)
- 数据库 (2)
- 摄影 (0)
- SQL (1)
- Google App Engine (1)
- linux (5)
- Eclipse plugin (10)
- Testing (0)
- Portal (0)
- 移动互联网 (0)
- SWTBot (1)
最新评论
-
江奇缘:
不错!!!!!!
web.xml里<filter-mapping>中的<dispatcher>作用 -
yy8093:
commonj 第三步,那个调用的方法要在哪里调?servle ...
JAVA中多种计时器的比较与分析 -
di1984HIT:
学习了,不错~
web.xml里<filter-mapping>中的<dispatcher>作用 -
penkee:
com.lowagie.text.DocumentExcept ...
iText中输出 中文 -
氵壞男亼乀:
我想请问下 你哪个html里面引入的几个js文件没看懂!你 ...
DWR入门教程之HelloWorld
Global variables are evil. Within YUI, we use only two globals: YAHOO
and YAHOO_config
. Everthing in YUI makes use of members within the YAHOO
object hierarchy or variables that are scoped to such a member. We advise that you exercise similar discipline in your own applications, too.
Douglas Crockford has been teaching a useful singleton pattern for achieving this discipline, and I thought his pattern might be of interest to those of you building on top of YUI. Douglas calls this the “module pattern.” Here’s how it works:
1. Create a namespace object: If you’re using YUI, you can use the YAHOO.namespace()
method:
YAHOO.namespace("myProject");
This assigns an empty object myProject
as a member of YAHOO
(but doesn’t overwrite myProject
if it already exists). Now we can begin adding members to YAHOO.myProject
.
2. Assign the return value of an anonymous function to your namespace object:
YAHOO.myProject.myModule = function () {
return {
myPublicProperty: "I'm accessible as YAHOO.myProject.myModule.myPublicProperty.";
myPublicMethod: function () {
YAHOO.log("I'm accessible as YAHOO.myProject.myModule.myPublicMethod.");
}
};
}(); // the parens here cause the anonymous function to execute and return
Note the very last line with the closing curly brace and then the parentheses ()
— this notation causes the anonymous function to execute immediately, return
ing the object containing myPublicProperty
and myPublicMethod
. As soon as the anonymous function returns, that returned object is addressable as YAHOO.myProject.myModule
.
3. Add “private” methods and variables in the anonymous function prior to the return
statement. So far, the above code hasn’t bought us any more than we could have gotten by assigning myPublicProperty
and myPublicMethod
directly to YAHOO.myProject.myModule
. But the pattern does provide added utility when we place code before the return
statement:
YAHOO.myProject.myModule = function () {
//"private" variables:
var myPrivateVar = "I can be accessed only from within YAHOO.myProject.myModule.";
//"private" method:
var myPrivateMethod = function () {
YAHOO.log("I can be accessed only from within YAHOO.myProject.myModule");
}
return {
myPublicProperty: "I'm accessible as YAHOO.myProject.myModule.myPublicProperty."
myPublicMethod: function () {
YAHOO.log("I'm accessible as YAHOO.myProject.myModule.myPublicMethod.");
//Within myProject, I can access "private" vars and methods:
YAHOO.log(myPrivateVar);
YAHOO.log(myPrivateMethod());
//The native scope of myPublicMethod is myProject; we can
//access public members using "this":
YAHOO.log(this.myPublicProperty);
}
};
}(); // the parens here cause the anonymous function to execute and return
In the codeblock above, we’re return
ing from an anonymous function an object with two members. These members are addressable from within YAHOO.myProject.myModule
as this.myPublicProperty
and this.myPublicMethod
respectively. From outside of YAHOO.myProject.myModule
, these public members are addressable as YAHOO.myProject.myModule.myPublicProperty
and YAHOO.myProject.myModule.myPublicMethod
.
The private variables myPrivateProperty
and myPrivateMethod
can only be accessed from within the anonymous function itself or from within a member of the return
ed object. They are preserved, despite the immediate execution and termination of the anonymous function, through the power of closure — the principle by which variables local to a function are retained after the function has returned. As long as YAHOO.myProject.myModule
needs them, our two private variables will not be destroyed.
4. Do something useful with the pattern. Let’s look at a common use case for the module pattern. Suppose you have a list, some of whose list items should be draggable. The draggable items have the CSS class draggable
applied to them.
- <!--This script file includes all of the YUI utilities:-->
- <script type="text/javascript" src="http://yui.yahooapis.com/2.2.2/build/utilities/utilities.js"></script>
- <ul id="myList">
- <li class="draggable">Item one.</li>
- <li>Item two.</li> <!--item two won't be draggable-->
- <li class="draggable">Item three.</li>
- </ul>
- <script>
- YAHOO.namespace("myProject");
- YAHOO.myProject.myModule = function () {
- //private shorthand references to YUI utilities:
- var yue = YAHOO.util.Event,
- yud = YAHOO.util.Dom;
- //private method:
- var getListItems = function () {
- //note that we can use other private variables here, including
- //our "yud" shorthand to YAHOO.util.Dom:
- var elList = yud.get("myList");
- var aListItems = yud.getElementsByClassName(
- "draggable", //get only items with css class "draggable"
- "li", //only return list items
- elList //restrict search to children of this element
- );
- return aListItems;
- };
- //the returned object here will become YAHOO.myProject.myModule:
- return {
- aDragObjects: [], //a publicly accessible place to store our DD objects
- init: function () {
- //we'll defer making list items draggable until the DOM is fully loaded:
- yue.onDOMReady(this.makeLIsDraggable, this, true);
- },
- makeLIsDraggable: function () {
- var aListItems = getListItems(); //these are the elements we'll make draggable
- for (var i=0, j=aListItems.length; i<j; i++) {
- this.aDragObjects.push(new YAHOO.util.DD(aListItems[i]));
- }
- }
- };
- }(); // the parens here cause the anonymous function to execute and return
- //The above code has already executed, so we can access the init
- //method immediately:
- YAHOO.myProject.myModule.init();
- </script>
This example is a simple one, and it’s deliberately verbose — if this was all we were doing, we could doubtless write it in a more compact way. However, this pattern scales well as the project becomes more complex and as its API grows. It stays out of the global namespace, provides publicly addressable API methods, and supports protected or “private” data and methods along the way.
发表评论
-
Ext-API详解--core/Ext.js
2009-05-08 14:43 23251、Ext.apply (Object obj, Objec ... -
Ext form提交的问题及解决
2008-10-22 00:44 3815近日做Ext的form提交,遇到一些问题,聊以志之。 ... -
Ext的日期格式说明
2008-10-17 15:24 2463Ext的日期格式说明,很实用的,如果大家遇到这方面的问题,可以 ... -
Ext学习之HelloWorld
2008-09-14 16:27 36931.首先,要下载ext,解压后,将其存放到WebRoot目录下 ... -
Ext继承关系 讲解
2008-09-12 15:50 4665Ext提供了这样的一个实用函数 Ext.extend (AP ... -
ECMAScript原始类型精讲
2008-05-12 10:17 1379为了更好的深入了解Java ... -
Ext Demo 汇总
2008-04-07 10:32 82791.Mail demo: http://www.demo.nb ... -
Eclipse中安装Spket插件
2008-04-01 14:29 12551Start Aptana and navigate the a ... -
Ext中的享元模式(flyweight)
2008-03-27 18:11 2617今天看Ext的sample,不明白下面一段话: Ext.fl ... -
Ext inheritance machnism
2008-03-13 11:37 1604Ext provides a utility function ... -
Ext中页面标签与组件的转化
2008-03-07 15:55 2032在ext中,页面上的标签如果编写合适,可以自动转化为Ext的组 ... -
使用Ext时需要注意的guideline
2007-12-10 15:49 14761.comments, Sure, your applicat ... -
Ext中的Module模式
2007-12-08 12:02 2157在Ext中大量应用了Module pattern,对于新手而言 ... -
Ext中namespace的作用
2007-12-07 23:58 11643Ext中在每一个页面中添加一个namespace呢,就像下面的 ... -
临发布2.0前对ExtJS作者Jack Slocum的访谈
2007-10-15 14:48 1555在Ext下一个版本的预览 ...
相关推荐
模块模式 Javascript介绍这个项目只是为了测试模块模式javascript要求安装$ git clone git@github.com:kiki-le-singe/module-pattern-javascript.git $ bower install包含的 JavaScript 库来源
js-module-pattern-test 这是 MakerSquare 作为学习 AJAX 的工具给出的。 因为我已经有了 AJAX 的经验,所以我借此机会学习和使用了 JavaScript 模块模式。 共有三个模块。 显示、电影和 API。 显示和 API 功能如...
Use several options for writing modular code—including the Module pattern, Asyncronous Module Definition (AMD), and CommonJS Discover design patterns implemented in the jQuery library Learn popular ...
Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, ...
Even JavaScript developers are now interested in building programs in a modular pattern. Modules help people who aren’t yet familiar with code to find what they are looking for and also makes it ...
数据模块模式项目 一个尝试使用data- *属性将原始HTML与Javascript绑定以创建动态... Return a cleanup function that will be used to remove any code that must be cleaned up, i.e., event listeners. */ return
模块化模式在javascript世界中,有许多编程工具,唯一的细节就是滥用其工具。 最后,您将获得分布在整个代码中且没有顺序的变量和函数,这最终是无法维护的。 编写代码时的最佳实践之一是使用javascript中的设计模式...
6. **模块模式(Module Pattern)** 这种模式结合了私有变量和公共接口,提供了封装和隐藏实现细节的能力。 ```javascript var person = (function() { var _name = 'Eve'; var _age = 28; function sayHello...
正则表达式用于模式匹配和文本操作,`/pattern/flags`定义正则,`test()`检查匹配,`exec()`执行搜索并返回结果数组,`match()`、`search()`、`replace()`、`split()`在字符串中应用正则表达式。 8. **数学Math**...
- **Composite Pattern(组合模式)**:将对象组合成树形结构以表示部分-整体层次结构。 - **适配器模式**:允许不兼容的接口之间可以协同工作。 - **观察者模式**:在对象之间定义一对多的依赖关系,当一个对象的...
3. revealing module pattern:暴露模块内部变量和函数的一种方式,提高代码组织性和可维护性。 4. 工厂函数与类模式:在没有类的JavaScript中,模拟面向对象编程。 5. 动态代理模式:通过Proxy对象实现对目标对象的...
数据模块模式(Data Module Pattern)是一种在JavaScript应用中组织代码的方式,它强调将数据与处理数据的逻辑分离,以提高代码的可维护性和可重用性。在这个名为"data-module-pattern-demo"的项目中,Viget公司提供...
1. **模块化编程**:书中会介绍如何使用命名空间、立即执行函数表达式(IIFE)以及AMD(Asynchronous Module Definition)和CommonJS模块加载机制来组织代码,避免全局变量污染,提高代码的可复用性和可测试性。...
5. **模块模式(Module Pattern)**: 模块模式是JavaScript中的一种设计模式,它允许我们创建私有变量和方法,同时提供公共接口。 ```javascript var PersonModule = (function() { var name = "John", age = ...
$ cd js_module_pattern_tracalorie 用法 有4个控制器(模块)可以创建此应用程序: 为了与本地存储进行通信,添加,编辑,删除项StorageCtrl控制器 用于控制膳食及其卡路里的ItemCtrl控制器 用于操纵UI UICtrl控制...
var Module = (function() { var privateVar = 'private'; function privateMethod() {} var publicVar = 'public'; function publicMethod() {} return { publicVar: publicVar, publicMethod: public...
1. **ES Module 方式** ```javascript import { pinyin } from 'pinyin-pro'; const result = pinyin('汉语拼音'); // 'hànyǔpīnyīn' ``` 2. **CommonJS 方式** ```javascript const { pinyin } = ...