js设计模式(1)

原创
2012/08/17 09:10
阅读数 161
<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>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部