JavaScript 事件對象的實現
更新時間:2009年07月13日 00:38:27 作者:
前我寫過一篇關于JavaScript如何實現面向對象編程的文章。今天,我寫這篇文章跟大家討論一下,如何實現事件。
比如,我們定義了一個Classroom對象,這里我們定一個事件,當教室里的人增加超60人時就觸發(fā)一個事件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);
復制代碼 代碼如下:
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);
相關文章
滾動條的監(jiān)聽與內容隨著滾動條動態(tài)加載的實現
下面小編就為大家?guī)硪黄獫L動條的監(jiān)聽與內容隨著滾動條動態(tài)加載的實現。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
js中document.referrer實現移動端返回上一頁
本文主要介紹了document.referrer實現移動端返回上一頁的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02

