1. 模块编程模式的启示(Revealing Module Pattern)
  2. 客户端对象(Custom Objects)
  3. 懒函数定义(Lazy Function Definition)

Christian 不喜欢module pattern,并对此编程模式进行了研究.在此基础上想出了一些新的东西他称之为 Revealing Module Pattern. 正如其名, 这种模式来源于Module Pattern, 但相比之下结构感更强且利于理解,尤其是在开发团队中将自己的代码移交给别人更容易上手.

首先,他还是一种基础的function定义与回调:

var anchorChange4 = function () {}();

其次,定义属性与方法时我们不用花太多的时间去在意 他们是否是 private 或者 public:

 // this will be a private property
var config = {
colors: [ "#F63", "#CC0", "#CFF" ]
}
// this will be a public method
var init = function () {
var self = this;
}
// assign reference to current object to "self"
// get all links on the page
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
for (var i = 0; i < size; i++) {
anchors[i].color = config.colors[i];
anchors[i].onclick = function () {
self.changeColor(this, this.color);
// this is bound to the anchor object
return false;
};
}
// this will be a public method
var changeColor = function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
}

现在我们说一下核心部分.切换到 Module Pattern模式下, 你已经注意到了在结束时return 语句中包含public 属性和方法. 在 Revealing Module Pattern模式下, 你只需要放入共享的属性与方法即可:

 return {
// declare which properties and methods are supposed to be public
init: init,
changeColor: changeColor
}

只有2个公共属性需要初始化一个是 init (负责编译前的准备工作) 和 changeColor (负责函数调用时的 clicked事件). 这样代码保持了整洁:

  // revealing module pattern
var anchorChange4 = function () {
// this will be a private property
var config = { colors: [ "#F63", "#CC0", "#CFF" ] }
// this will be a public method
var init = function () {
var self = this;
}
// assign reference to current object to "self"
// get all links on the page
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
for (var i = 0; i < size; i++) {
anchors[i].color = config.colors[i];
anchors[i].onclick = function () {
self.changeColor(this, this.color);
// this is bound to the anchor object
return false;
};
} // this will be a public method
var changeColor = function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
} return {
// declare which properties and methods are supposed to be public
init: init,
changeColor: changeColor
}
} ();

与所有的编程模式一样, 你需要在script代码快中调用:

 <script type="text/javascript">
anchorChange4.init();
</script>

客户端对象(Custom Objects)

创建一类对象在面向对象的语言中很常见. 但是Javascript与面向对象相比较是基于对象的语言. 在JavaScript中, 在接受参数创建对象时你必须调用  function 构造器 .你不能像java 一样有类与子类( Java ).

首先, 我们需要为我们的对象创建函数构造器:

var anchorChanger = function () {};

我们初始化了一个空的函数构造器,稍后为对象添加 init 方法.

使用原型, 对我们的对象可以添加各种属性且可以访问. 最后设置了 3 属性, 也就是: config, changeColor and init.

    anchorChanger.prototype.config = {
colors: [ "#F63", "#CC0", "#CFF" ]
} anchorChanger.prototype.changeColor =
function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
}; anchorChanger.prototype.init = function () {
var self = this;
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
for (var i = 0; i < size; i++) {
anchors[i].color = self.config.colors[i];
anchors[i].onclick = function () {
self.changeColor(this, this.color);
return false;
};
}
};

Mike West指出, 使用原型的效率会很高.他不必每次都创建对象访问对象.

    // custom object constructor
var anchorChanger = function () {
this.init();
};
anchorChanger.prototype.config = {
colors: [ "#F63", "#CC0", "#CFF" ]
}
anchorChanger.prototype.changeColor = function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
};
anchorChanger.prototype.init = function () {
var self = this;
}
// get all links on the page
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
for (var i = 0; i < size; i++) {
anchors[i].color = self.config.colors[i];
anchors[i].onclick = function () {
self.changeColor(this, this.color);
return false;
};
} ;

在页面中我们只需要声明一个 anchorChanger的接口即可:

<script type="text/javascript">
new anchorChanger();
</script>

惰性函数定义(Lazy Function Definition)

Peter Michaux 提出了 Lazy Function Definition 模式. 当你在页面中反复计算、调用函数多次时这种模式很实用.在这种模式下你要确定你的方法只做了一次,且仅仅做了一次.例子中 Peter 给出了在不同浏览器宿主环境不同而处理滚动的功能也不同.在不同浏览器模型中, 当函数在第一时间被调用时,滚动功能也不同(此处参看lazy-function-definition-pattern).

对于我们的任务而言,这种模式没有提供任何的优势,因为在这里我们没有繁重复杂的计算. 我们仅仅是展示一下这种模式是如何工作的:

var anchorChange5 = function () {};

像别的模式一样添加一些功能,代码如下:

  // define configuration
var config = {
colors: [ "#F63", "#CC0", "#CFF" ]
};
// get all links
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
// loop through anchors and attach events
for (var i = 0; i < size; i++) {
anchors[i].color = config.colors[i];
anchors[i].onclick = function ()
{ anchorChange5().changeColor(this, this.color);
return false;
};
}

以上代码确保在调用时运行一次,所以不需要包含随后调用函数的性质.在 Lazy Function Definition 模式,需要从新定义函数声明:

  // redefine function so that it only holds the changeColor function
anchorChange5 = function () {
return {
changeColor: function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
}
};
};

所以懒函数定义模式代码如下:

 // lazy function definition
var anchorChange5 = function () {
// define configuration
var config = { colors: [ "#F63", "#CC0", "#CFF" ] };
// get all links
var anchors = document.getElementsByTagName("a");
var size = anchors.length;
// loop through anchors and attach events
for (var i = 0; i < size; i++) {
anchors[i].color = config.colors[i];
anchors[i].onclick = function () {
anchorChange5().changeColor(this, this.color);
return false;
};
}
// redefine function so that it only holds the changeColor function
anchorChange5 = function () {
return { changeColor: function (linkObj, newColor) {
linkObj.style.backgroundColor = newColor;
} };
};
};

发生了什么:

  1. anchorChange5 在页面上调用和定义 config 变量与 anchors集合
  2. 每一个 anchor会被粘贴上一个onclick事件, 当被触发调用 changeColor方法并且从新声明 anchorChange5 function
  3. 在此之后, anchorChange5会从新声明并可以访问到只持有 changeColor 方法

在body之间,要调用 anchorChange5 :

<script type="text/javascript">
anchorChange5();
</script>

Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)的更多相关文章

  1. 游戏编程模式 Game Programming Patterns (Robert Nystrom 著)

    第1篇 概述 第1章 架构,性能和游戏 (已看) 第2篇 再探设计模式 第2章 命令模式 (已看) 第3章 享元模式 (已看) 第4章 观察者模式 (已看) 第5章 原型模式 (已看) 第6章 单例模 ...

  2. Javascript编程模式(JavaScript Programming Patterns)Part 1.(初级篇)

    JavaScript 为网站添加状态,这些状态可能是校验或者更复杂的行为像拖拽终止功能或者是异步的请求webserver (aka Ajax). 在过去的那些年里, JavaScript librar ...

  3. JavaScript 编程模式

    编程模式,是源自经验和探索总结出的最佳实践方案,既有助于可读性和可维护性,也有助于提升整体性能. 行为隔离 总则:结构.样式和行为之间两两隔离. 避免在结构中使用内联事件 尽量少用 <scrip ...

  4. JavaScript编程:javaScript核心基础语法

    1.javaScript核心基础语法: javaScript技术体系包含了5个内容:          1.核心语言定义:          2.原生对象和雷子对象:          3.浏览器对象 ...

  5. javascript常见编程模式举例

    近期买到手了一本<javascript框架设计>,具体介绍开发js框架所用到的知识.初读一点,乐帝脆弱的理论修养就暴露无遗了,所以专门加强理论修养,重看javascript编程模式的举例. ...

  6. JQuery日记6.5 Javascript异步模式(一)

    理解力JQuery前实现异步队列,有必要理解javascript异步模式. Javascript异步其实并不严重格异步感,js使某些片段异步方式在将来运行,流不必等待继续向下进行. 在多线程的语言中最 ...

  7. JavaScript严谨模式(Strict Mode)

    下面的内容翻译自It’s time to start using JavaScript strict mode,作者Nicholas C.Zakas参与了YUI框架的开发,并撰写了多本前端技术书籍,在 ...

  8. javaScript设计模式之面向对象编程(object-oriented programming,OOP)(一)

    面试的时候,总会被问到,你对javascript面向对象的理解? 面向对象编程(object-oriented programming,OOP)是一种程序设计范型.它讲对象作为程序的设计基本单元,讲程 ...

  9. .Net Core自实现CLR异步编程模式(Asynchronous programming patterns)

    最近在看一个线程框架,对.Net的异步编程模型很感兴趣,所以在这里实现CLR定义的异步编程模型,在CLR里有三种异步模式如下,如果不了解的可以详细看MSDN 文档Asynchronous progra ...

随机推荐

  1. 抽象(abstract)升级版变接口(interface) 继承(extends)升级版叫实现(implements) 升级版啊升级版 接口可以多继承

    Client --------------------------------------------------- public class Client{ public static void m ...

  2. hasLayout与Block formatting contexts的学习(下)

    BFC布局规则: 内部的Box会在垂直方向,一个接一个地放置. Box垂直方向的距离由margin决定.属于同一个BFC的两个相邻Box的margin会发生重叠 每个元素的margin box的左边, ...

  3. PHP之数组函数归类

    数组之所以强大,除了本身声明.存储方式灵活,它还有坚强后盾:一系列功能各异的数组处理函数.就像一只军队,除了领队将军本身能征善战,指挥英明之外,还有一群不怕死.忠实于他的士兵,这样才能显得整体的强大. ...

  4. oracle 11G创建表空间、用户、配置监听和TNS

    最近总在安装各种版本的oralce数据库做测试,11G,32位的,64位的,12C的,每次都折腾表空间,用户.tns啥的,这里记录下,再也不用现用现百度找了 一.创建表空间.用户  在plsql工具中 ...

  5. 第三篇、FMDB使用

    简介: FMDB是基于SQlite3的封装一个第三方的OC库,操作起来更加简单,性能比Coredata更加高. 1.创建sqlite文件 2.导入FMDB头文件 3.创建数据库表table 4.编写s ...

  6. 模板:qsort+bsearch应用

    (1)qsort: 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)( ...

  7. php json_encode()和json_decode()

    json_encode()和json_decode()分别是编译和反编译过程 注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者nu ...

  8. centos6.5 安装python2.7.5

    1. 下载python2.7.5,保存到 /data/http://www.python.org/ftp/python/ 2. 解压文件tar xvf Python-2.7.5.tar.bz2 3. ...

  9. Linux---vi编辑器必会操作

    移动光标: (1)基本的上下左右:通过箭头按键控制 (2)跳到一行的末尾:键盘"end" (3)跳到一行的开头:键盘"home" (4)跳到最后一行:shift ...

  10. Web前端新人笔记之jquery入门

    本章将为大家介绍以下几点内容: 1.jquery的主要特点: 2.建立jquery的编码环境: 3.简单jquery脚本示例: 4.选择jquery而不是纯javaScript的理由: 5.常用的jq ...