js设计模式(1)
2012-08-17 00:00
411 查看
<script type="text/javascript">
//形式一:
/* Anim class. */
var Anim = function() { };
Anim.prototype.start = function() { };
Anim.prototype.stop = function() { };
/* Usage. */
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
//形式二:
/* Anim class, with a slightly different syntax for declaring methods. */
var Anim = function() { };
Anim.prototype = {
start: function() { },
stop: function() { }
};
//形式3:
/* Add a method to the Function object that can be used to declare methods. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
};
/* Anim class, with methods created using a convenience method. */
var Anim = function() { };
Anim.method('start', function() { });
Anim.method('stop', function() { });
//形式4:
/* This version allows the calls to be chained. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};
/* Anim class, with methods created using a convenience method and chaining. */
var Anim = function() { };
Anim.method('start', function() { }).method('stop', function() { });
//形式5:
/* Start and stop animations using functions. */
function startAnimation() { }
function stopAnimation() { }
</script>
//形式一:
/* Anim class. */
var Anim = function() { };
Anim.prototype.start = function() { };
Anim.prototype.stop = function() { };
/* Usage. */
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
//形式二:
/* Anim class, with a slightly different syntax for declaring methods. */
var Anim = function() { };
Anim.prototype = {
start: function() { },
stop: function() { }
};
//形式3:
/* Add a method to the Function object that can be used to declare methods. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
};
/* Anim class, with methods created using a convenience method. */
var Anim = function() { };
Anim.method('start', function() { });
Anim.method('stop', function() { });
//形式4:
/* This version allows the calls to be chained. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};
/* Anim class, with methods created using a convenience method and chaining. */
var Anim = function() { };
Anim.method('start', function() { }).method('stop', function() { });
//形式5:
/* Start and stop animations using functions. */
function startAnimation() { }
function stopAnimation() { }
</script>
相关文章推荐
- [js高手之路]设计模式系列课程-单例模式实现模态框
- js设计模式-封装
- JS设计模式——4.继承(概念)
- JS常用的设计模式(5)——代理模式
- 【js设计模式笔记---装饰者模式】
- js原生设计模式——3简单工厂模式\简单工厂模式封装简单对象
- JS-Javascript 迭代器设计模式 Iterator pattern
- JS设计模式一:单例模式
- js原生之设计模式开篇介绍
- JS几种常用对象设计模式
- js 设计模式学习(3)
- JS设计模式之单例模式
- [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
- JS常用的设计模式(17)—— 状态模式
- JS观察者设计模式:实现iframe之间快捷通信
- js基础对象-编程模式与设计模式
- js设计模式之模板方法模式
- JS 对象关联设计模式 比 面向对象设计更优秀
- js设计模式之节流器模式
- js原生设计模式——12装饰者模式