For example, we define a Classroom object. Here we define an event. When the number of people in the classroom increases by more than 60, an event onFull is triggered; the specific definition is as follows:
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("There are " number "people" in the classroom);
}
function show2(number)
{
alert("The classroom exceeds" (number-60) "people");
}
var classroom1=new Classroom();
classroom1.onFull=show1;
classroom1.peopleEnter(30);
classroom1.peopleEnter(32);
classroom1.onFull=show2;
classroom1.peopleEnter(34);