JavaScript 事件對象的實現(xiàn)
更新時間:2009年07月13日 00:38:27 作者:
前我寫過一篇關于JavaScript如何實現(xiàn)面向對象編程的文章。今天,我寫這篇文章跟大家討論一下,如何實現(xiàn)事件。
比如,我們定義了一個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)聽與內(nèi)容隨著滾動條動態(tài)加載的實現(xiàn)
下面小編就為大家?guī)硪黄獫L動條的監(jiān)聽與內(nèi)容隨著滾動條動態(tài)加載的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
淺談es6 javascript的map數(shù)據(jù)結構
本篇文章主要介紹了淺談es6 javascript的map數(shù)據(jù)結構,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
js下通過prototype擴展實現(xiàn)indexOf的代碼
這里使用js prototype擴展實現(xiàn)的indexOf的實現(xiàn)代碼,跟js自帶的方法,差不多。2010-12-12
js中document.referrer實現(xiàn)移動端返回上一頁
本文主要介紹了document.referrer實現(xiàn)移動端返回上一頁的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02

