Home  >  Article  >  Web Front-end  >  Implementation of JavaScript event object_javascript skills

Implementation of JavaScript event object_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:50:121388browse

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:

Copy the code The code 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);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn