1、类的创建
prototype.js已经封装好了,这个很简单。
var Person=Class.create();
Person.prototype=
{
initialize:function()
{
}
};可以看到在使用这样的方式定义class后,它和javascript原来的通过function方式来定义一个类就有明确的区分了,在这种情况下我们就可以用Class.create来定义类,用function来直接定义函数。
类通常还涉及静态成员(static性质的)和实例成员(需要实例化才可调用)的定义。
在javascript中这点也非常容易:
静态成员:

var Person=
{
name:'person',
getName:function()
{return 'person'}
};
Person.prototype=
{
childname:'child',
eat:function()
}2、类的继承
类的继承其实javascript本身就支持的,不过prototype提供了一种另外的方法。
按照javascript的支持的实现:
var Student=Class.create();
Student.prototype=new Person();在使用prototype的情况下可以这么实现:
var Student=Class.create();
Object.extend(Student.prototype,Person.prototype);
Student.prototype.study=function()
{};
Object.extend(Student.prototype,
{
study:function()
{}
});在事件机制上则碰到了一些疑惑,作为事件机制主要需要提供事件的定义,对于事件的监听以及对于事件的观察。
在javascript中事件需要以on开头,也就是作为事件就需要采用onclick这样类似的命名:
对上面的Student增加一个对外的事件,如:

Student.prototype.study=function()
{
this.onstudy();
}
Student.prototype.onstudy=function()
{};
function studyThis()
{
alert("study this");
}
var student=new Student();
student.onstudy=studyThis();
study.onstudy=watchStudy.bindAsEventListener(this);
function watchStudy(event)
{
alert("watch study");
}
本文介绍了Prototype.js框架的基础使用方法,包括类的创建与继承、事件机制等,并提供了具体的代码示例。

被折叠的 条评论
为什么被折叠?



