Prototype.js作为javascript的成功的开源框架,封装了很多好用的功能,虽然官方没提供什么文档,不过在google上一搜,好多相关的文档,不过在学习使用的过程中还是碰到了一些问题,希望熟悉的朋友能多加指点,对于prototype.js学习我关注这么几点,同时针对每点也讲讲学习的结果和碰到的问题,^_^
1、类的创建
prototype.js已经封装好了,这个很简单。
 var Person=Class.create();
      这样就创建了一个Person类,这个Person类必须提供initialize方法的实现:
var Person=Class.create();
      这样就创建了一个Person类,这个Person类必须提供initialize方法的实现:
 Person.prototype={
Person.prototype={
 initialize:function(){
                 initialize:function(){
 }
                 }
 };
      对比java,Class.create相当于Class.forName(),initialize相当于构造器,和java的构造器一样,可以自定义为带参数性质的。
      };
      对比java,Class.create相当于Class.forName(),initialize相当于构造器,和java的构造器一样,可以自定义为带参数性质的。
可以看到在使用这样的方式定义class后,它和javascript原来的通过function方式来定义一个类就有明确的区分了,在这种情况下我们就可以用Class.create来定义类,用function来直接定义函数。
类通常还涉及静态成员(static性质的)和实例成员(需要实例化才可调用)的定义。
在javascript中这点也非常容易:
静态成员: var Person={
var Person={
 name:'person',
                name:'person',
 getName:function(){return 'person'}
                getName:function(){return 'person'}
 };
      实例成员:
      };
      实例成员:    
 Person.prototype={
Person.prototype={
 childname:'child',
               childname:'child',
 eat:function()
               eat:function()
 }
      上面的Person.getName是可以直接这么调用的,但eat方法则需通过var person=new Person();person.eat();的方式来调用。
      }
      上面的Person.getName是可以直接这么调用的,但eat方法则需通过var person=new Person();person.eat();的方式来调用。
2、类的继承
类的继承其实javascript本身就支持的,不过prototype提供了一种另外的方法。
按照javascript的支持的实现:
 var Student=Class.create();
var Student=Class.create();
 Student.prototype=new Person();
      这样就实现了Student继承至Person。
      Student.prototype=new Person();
      这样就实现了Student继承至Person。
在使用prototype的情况下可以这么实现:
 var Student=Class.create();
      var Student=Class.create();
 Object.extend(Student.prototype,Person.prototype);
      子类要增加方法时可使用
      Object.extend(Student.prototype,Person.prototype);
      子类要增加方法时可使用 
 Student.prototype.study=function(){};
      或
Student.prototype.study=function(){};
      或 
 Object.extend(Student.prototype,{
Object.extend(Student.prototype,{
 study:function(){}
                                                                   study:function(){}
 });
3、事件机制(对类方法执行的监听和观察)
                                                          });
3、事件机制(对类方法执行的监听和观察)
在事件机制上则碰到了一些疑惑,作为事件机制主要需要提供事件的定义,对于事件的监听以及对于事件的观察。
在javascript中事件需要以on开头,也就是作为事件就需要采用onclick这样类似的命名:
对上面的Student增加一个对外的事件,如: Student.prototype.study=function(){
Student.prototype.study=function(){
 this.onstudy();
             this.onstudy();
 }
      }
 Student.prototype.onstudy=function(){};
      这个onstudy就是交给相应的实例去实现的,例如实例采用这样的方式:
      Student.prototype.onstudy=function(){};
      这个onstudy就是交给相应的实例去实现的,例如实例采用这样的方式: 
 function studyThis(){
      function studyThis(){
 alert("study this");
          alert("study this");
 }
      }
 var student=new Student();
      var student=new Student();
 student.onstudy=studyThis();
      对于事件通常都希望进行监听和观察,根据prototype提供的bindAsEventListener以及Observe,这么进行了尝试:
      student.onstudy=studyThis();
      对于事件通常都希望进行监听和观察,根据prototype提供的bindAsEventListener以及Observe,这么进行了尝试:
 study.onstudy=watchStudy.bindAsEventListener(this);
study.onstudy=watchStudy.bindAsEventListener(this);
 function watchStudy(event){
      function watchStudy(event){
 alert("watch study");
            alert("watch study");
 }
      }
1、类的创建
prototype.js已经封装好了,这个很简单。
 var Person=Class.create();
var Person=Class.create(); Person.prototype={
Person.prototype={ initialize:function(){
                 initialize:function(){ }
                 } };
      };可以看到在使用这样的方式定义class后,它和javascript原来的通过function方式来定义一个类就有明确的区分了,在这种情况下我们就可以用Class.create来定义类,用function来直接定义函数。
类通常还涉及静态成员(static性质的)和实例成员(需要实例化才可调用)的定义。
在javascript中这点也非常容易:
静态成员:
 var Person={
var Person={ name:'person',
                name:'person', getName:function(){return 'person'}
                getName:function(){return 'person'} };
      }; Person.prototype={
Person.prototype={ childname:'child',
               childname:'child', eat:function()
               eat:function() }
      }2、类的继承
类的继承其实javascript本身就支持的,不过prototype提供了一种另外的方法。
按照javascript的支持的实现:
 var Student=Class.create();
var Student=Class.create(); Student.prototype=new Person();
      Student.prototype=new Person();在使用prototype的情况下可以这么实现:
 var Student=Class.create();
      var Student=Class.create(); Object.extend(Student.prototype,Person.prototype);
      Object.extend(Student.prototype,Person.prototype); Student.prototype.study=function(){};
Student.prototype.study=function(){}; Object.extend(Student.prototype,{
Object.extend(Student.prototype,{ study:function(){}
                                                                   study:function(){} });
                                                          });在事件机制上则碰到了一些疑惑,作为事件机制主要需要提供事件的定义,对于事件的监听以及对于事件的观察。
在javascript中事件需要以on开头,也就是作为事件就需要采用onclick这样类似的命名:
对上面的Student增加一个对外的事件,如:
 Student.prototype.study=function(){
Student.prototype.study=function(){ this.onstudy();
             this.onstudy(); }
      } Student.prototype.onstudy=function(){};
      Student.prototype.onstudy=function(){}; function studyThis(){
      function studyThis(){ alert("study this");
          alert("study this"); }
      } var student=new Student();
      var student=new Student(); student.onstudy=studyThis();
      student.onstudy=studyThis(); study.onstudy=watchStudy.bindAsEventListener(this);
study.onstudy=watchStudy.bindAsEventListener(this); function watchStudy(event){
      function watchStudy(event){ alert("watch study");
            alert("watch study"); }
      } 
                     
                    
                 
                    
                 


 initialize:
                 initialize: 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号