---
title: "javascript good good study,day day up"
author: "ganjp"
date: "2011-09-21T15:04:59Z"
tags: []
categories: ["Web前端"]
slug: "javascript-good-good-study-day-day-up"
draft: false
lastmod: "2011-09-21T15:04:59Z"
id: 1177103
url: "https://ganjp.iteye.com/blog/1177103"
---

写道

/\**  
\* 实现一个动画的开始和结束 可能编码有5方式  
\*/  
//the basic type:  
/*  
function start() {

}

function stop() {

}  
\*/

var Anim = function() {};

/\** the fisrt type:  
\*Anim.prototype.start = function() {}  
Anim.prototype.stop = function() {}  
\*/

//the second type:  
Anim.prototype = {  
//声明属性 和 方法 有点类型java的object的声明  
version : 'v1.00',  
start : function() {  
alert('start');  
return this;  
},  
stop: function() {  
alert('stop');  
return this; //每个方法都加上这个就可以链式的方式运行了。  
}  
};

//the third type:  
/\*给Function定义一个原型方法 Function.prototype.method允许我们为类添加新的方法 链式方法  
Function.prototype.method = function(name,fn) {  
this.prototype\[name] = fn;  
}  
Anim.method('start',function() {});  
Anim.method('end',function() {});  
\*/

//the four type:  
//another method to write  
Function.prototype.method = function(name,fn) {  
this.prototype\[name] = fn;  
return this; //返回对象本身  
}

Anim.method('begin',function() {  
alert('begin');  
}).method('end',function() {  
alert('end');  
});

var anim = new Anim();  
anim.begin();  
anim.end();  
anim.start().stop();