比如,我们定义了一个classroom对象,这里我们定一个事件,当教室里的人增加超60人时就触发一个事件onfull;具体定义如下:
复制代码 代码如下:
var classroom=function()
{
this.numberofpeople=0;
this.onfull=null;
this.peopleenter=function(number)
{
this.numberofpeople+=number;
if(this.numberofpeople>60&&this.onfull!=null)
{
this.onfull(this.numberofpeople);
}
}
}
function show1(number)
{
alert(教室里有+number+人);
}
function show2(number)
{
alert(教室里超出了+(number-60)+人);
}
var classroom1=new classroom();
classroom1.onfull=show1;
classroom1.peopleenter(30);
classroom1.peopleenter(32);
classroom1.onfull=show2;
classroom1.peopleenter(34);