JavaScript 装饰者模式(this运用)
例:
function ConcreteClass() {
this.performTask = function () {
this.preTask();
console.log('doing something');
this.postTask();
};
}
function AbstractDecorator(decorated) {
this.performTask = function () {
decorated.performTask();
};
}
function ConcreteDecoratorClass(decorated) {
this.base = AbstractDecorator;
this.base(decorated);
decorated.preTask = function () {
console.log('pre-calling..');
};
decorated.postTask = function () {
console.log('post-calling..');
};
}
var concrete = new ConcreteClass();
console.dir(concrete);
var decorator1 = new ConcreteDecoratorClass(concrete);
console.dir(decorator1);console.dir(concrete);
var decorator2 = new ConcreteDecoratorClass(decorator1);
console.dir(decorator2);console.dir(decorator1);
decorator2.performTask();

这个里面最难理解的就是
this.base=AbstractDecorator;
this.base(decorated);
开始还以为有base这个属性呢,其实base没有特殊的意思,换成其他的也一样,只是一个function
这两句要连接在一起运用。
2.
<script type="text/javascript">
var tree = {};
tree.decorate = function () {
console.log('Make sure the tree won\'t fall');
};
tree.getDecorator = function (deco) {
tree[deco].prototype = this;
return new tree[deco];
};
tree.RedBalls = function () {
this.decorate = function () {
this.RedBalls.prototype.decorate(); // 第7步:先执行原型(这时候是Angel了)的decorate方法
console.log('Put on some red balls'); // 第8步 再输出 red
// 将这2步作为RedBalls的decorate方法
}
};
tree.BlueBalls = function () {
this.decorate = function () {
this.BlueBalls.prototype.decorate(); // 第1步:先执行原型的decorate方法,也就是tree.decorate()
console.log('Add blue balls'); // 第2步 再输出blue
// 将这2步作为BlueBalls的decorate方法
}
};
tree.Angel = function () {
this.decorate = function () {
this.Angel.prototype.decorate(); // 第4步:先执行原型(这时候是BlueBalls了)的decorate方法
console.log('An angel on the top'); // 第5步 再输出angel
// 将这2步作为Angel的decorate方法
}
};
console.dir(tree);
tree = tree.getDecorator('BlueBalls');console.dir(tree); // 第3步:将BlueBalls对象赋给tree,这时候父原型里的getDecorator依然可用
tree = tree.getDecorator('Angel'); console.dir(tree); // 第6步:将Angel对象赋给tree,这时候父原型的父原型里的getDecorator依然可用
tree = tree.getDecorator('RedBalls'); console.dir(tree); // 第9步:将RedBalls对象赋给tree
tree.decorate(); // 第10步:执行RedBalls对象的decorate方法
</script>

这个很绕人的,估计也不怎么用的到,就是看看this的用法
大体的过程就是
1.有一个tree对象,有好多的方法
2.
tree.getDecorator = function (deco) {
tree[deco].prototype = this;
return new tree[deco];
};
这句话就是返回一个对象,该对象是deco的实例,deco是tree的方法中的一个,且最重要的该对象的prototype是旧tree对象,就是说该对象继承了旧tree对象,并由deco对它进行扩展。
3.例如上一步的deco就是BlueBalls
tree.BlueBalls = function () {
this.decorate = function () {
this.BlueBalls.prototype.decorate(); // 第1步:先执行原型的decorate方法,也就是tree.decorate()
console.log('Add blue balls'); // 第2步 再输出blue
// 将这2步作为BlueBalls的decorate方法
}
};
这一步就是对上一步对象的扩展
这个里面没有扩展,只有对旧方法的覆盖
this.BlueBalls.prototype.decorate();
这里的this.BlueBalls.prototype就是旧的tree对象

图就是上面的this对象,像个死循环,哦,本来就是个死循环
分析:
旧tree对象本身就有BlueBalls方法
新tree对象是旧tree中BlueBalls的实例,且旧tree中的BlueBalls的prototype就是个旧tree对象
很饶人

this 新tree对象
this.BlueBalls 新tree对象所对应的prototype(旧tree对象)中的BlueBails方法,也是新tree的constructor
this.BlueBails 就是旧tree对象
所有的就是这样一层一层套起来的
http://www.cnblogs.com/TomXu/archive/2012/02/24/2353434.html
JavaScript 装饰者模式(this运用)的更多相关文章
- 轻松掌握:JavaScript装饰者模式
装饰者模式 在传统的面向对象语言中,给对象添加功能常常使用继承的方式,但继承的方式会带来问题:当父类改变时,他的所有子类都将随之改变. 当JavaScript脚本运行时,在一个对象中(或他的原型上)增 ...
- javascript装饰器模式
装饰器模式 什么是装饰器 原名decorator 被翻译为装饰器 可以理解为装饰 修饰 包装等意 现实中的作用 一间房子通过装饰可以变得更华丽,功能更多 类似一部手机可以单独使用 但是很多人都愿意家个 ...
- JavaScript——装饰者模式
今天打算开始系统的学习设计模式,虽然之前有看过<大话设计模式>但是没能够静下心来写学习笔记导致很多内容都只是有一个概念而不会去应用.这次要记下学习的过程.接下来进入主题. 何为设计模式?设 ...
- JavaScript装饰者模式
这里我们通过需求逐渐引出装饰者模式. 下面是一个关于几代汽车的不同逐渐体现装饰者模式的. 首先,我们先引入一个接口文件----目的为检验实现类是否完全实现接口中的方法,代码如下, //定义一个静态方法 ...
- Javascript设计模式之装饰者模式详解篇
一.前言: 装饰者模式(Decorator Pattern):在不改变原类和继承的情况下动态扩展对象功能,通过包装一个对象来实现一个新的具有原对象相同接口的新的对象. 装饰者模式的特点: 1. 在不改 ...
- 【读书笔记】读《JavaScript设计模式》之装饰者模式
一.定义 装饰者模式可用来透明地把对象包装在具有同样接口的另一个对象之中.这样一来,你可以给一个方法添加一些行为,然后将方法调用传递给原始对象.相对于创建子类来说,使用装饰者对象是一种更灵活的选择(装 ...
- javascript设计模式学习之十五——装饰者模式
一.装饰者模式定义 装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.这种为对象动态添加职责的方式就称为装饰者模式.装饰者对象和它所装饰的对象拥有一致的接口,对于用 ...
- JavaScript高级---装饰者模式设计
一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...
- 再起航,我的学习笔记之JavaScript设计模式13(装饰者模式)
装饰者模式 装饰者模式(Decorator): 在不改变原对象的基础上,通过对其进行过包装拓展(添加属性高或者方法)使原有对象可以满足用户的更复杂需求. 如果现在我们有个需求,需要做一个提交表单,当我 ...
随机推荐
- idea 创建的maven+spring+mybatis项目整合 报错无法创建bean
报错如下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with n ...
- zookeeper源码导入
1 搭建步骤 1.1 到github中下载该项目 项目地址 https://github.com/apache/zookeeper.下载.zip包到本地解压. 解压后文件目录: 1.2 使用ant对源 ...
- mysql "order by" "distinct" "group by" "having"
本文用到的表结构 create table stu( stu_id int auto_increment primary key, name ) not null, age smallint, cls ...
- Selenium+Java元素定位之三
首先自己先准备一个表格代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 记两个国外CTF的弱pwn
两道题都来自CSAW CTF 18.PWN学得不够多,如果哪里错了,欢迎留言交流. 第一个题 get_it checksec检查之后,发现栈保护没开,很可能是栈溢出.IDA打开F5看伪源码. int ...
- [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [LeetCode] 243. Shortest Word Distance_Easy
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class
转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Colle ...
- AnsiString和各种数据类型间相互转换 [数据转换]
//Ansistring 转 char void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = " ...
- checkBox的使用和事件监听
直接上代码: <!DOCTYPE html> <html> <head> <title></title> </head> < ...