javascript-------------的灵活性

    javascript具有较强的灵活性,我们可以把代码写得很简单,也可以写得很复杂。我们可以采用函数式编程风格,也可以采用面向对象的风格书写你的代码。

    我们可以使用javascript模仿其他高级语言的编程模式和习惯。也可以使用javascript自己的模式,完成传统的较复杂的服务器编程语言具有的面向对象的特性。

    例:

过程式
使用类
 1 var Anim=function(){
2 ......代码.........
3 };
4 Anim.prototype.start=function(){
5 .......代码.......
6 }
7 Anim.prototype.stop=function(){
8 .......代码........
9 }
10
11 /*使用*/
12 var myAnim=new Anim();
13 myAnim.start();
14 ........
15 myAnim.stop();
把类封装在声明中
 1 var Anim=function(){
2 .....代码.....
3 };
4 Anim.prototype={
5 start:function(){
6 .....代码......
7 },
8 stop:funtion(){
9 ......代码.....
10 }
11 };
传统类
 1 Function.prototype.method=funtion(name,fn){
2 this.prototype[name]=fn;
3 };
4
5 var Anim=function(){
6 .......代码.........
7 };
8 Anim.method('start',function(){
9 .......代码........
10 });
11 Anim.method('stop',function(){
12 .......代码........
13 })

我们可以把类写的像jquery的链式调用方式;

源码
 1 Function.prototype.method=function(name,fn){
2 this.prototype[name]=fn;
3 return this;
4 };
5 var Anim=function(){
6 .......代码.........
7 };
8 Anim.method('start',function(){
9 ......代码......
10 }).method('stop',function(){
11 .......代码......
12 });

呵呵,开始学五笔,打字慢死了,写的说明少点,以后打字快了,再多写说明;



posted @ 2011-10-07 23:01  冯际成  阅读(459)  评论(3编辑  收藏  举报

返回顶部