JavaScript 事件對(duì)象的實(shí)現(xiàn)
更新時(shí)間:2009年07月13日 00:38:27 作者:
前我寫過(guò)一篇關(guān)于JavaScript如何實(shí)現(xiàn)面向?qū)ο缶幊痰奈恼隆=裉?,我寫這篇文章跟大家討論一下,如何實(shí)現(xiàn)事件。
比如,我們定義了一個(gè)Classroom對(duì)象,這里我們定一個(gè)事件,當(dāng)教室里的人增加超60人時(shí)就觸發(fā)一個(gè)事件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);
復(fù)制代碼 代碼如下:
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);
您可能感興趣的文章:
相關(guān)文章
滾動(dòng)條的監(jiān)聽(tīng)與內(nèi)容隨著滾動(dòng)條動(dòng)態(tài)加載的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇滾動(dòng)條的監(jiān)聽(tīng)與內(nèi)容隨著滾動(dòng)條動(dòng)態(tài)加載的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02淺談es6 javascript的map數(shù)據(jù)結(jié)構(gòu)
本篇文章主要介紹了淺談es6 javascript的map數(shù)據(jù)結(jié)構(gòu),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12JavaScript中的方法調(diào)用詳細(xì)介紹
這篇文章主要介紹了JavaScript中的方法調(diào)用詳細(xì)介紹,JavaScript中,如果function屬于一個(gè)對(duì)象,那么通過(guò)對(duì)象來(lái)訪問(wèn)該function的行為稱之為“方法調(diào)用”,需要的朋友可以參考下2014-12-12js下通過(guò)prototype擴(kuò)展實(shí)現(xiàn)indexOf的代碼
這里使用js prototype擴(kuò)展實(shí)現(xiàn)的indexOf的實(shí)現(xiàn)代碼,跟js自帶的方法,差不多。2010-12-12js中document.referrer實(shí)現(xiàn)移動(dòng)端返回上一頁(yè)
本文主要介紹了document.referrer實(shí)現(xiàn)移動(dòng)端返回上一頁(yè)的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02深入探究使JavaScript動(dòng)畫流暢的一些方法
這篇文章主要介紹了使JavaScript動(dòng)畫流暢的一些方法,包括與CSS動(dòng)畫效果的一些對(duì)比,需要的朋友可以參考下2015-06-06[JSF]使用DataModel處理表行事件的實(shí)例代碼
在使用JSF中,最常用的恐怕就要屬于表格的處理了。使用DataModel可以方便地進(jìn)行對(duì)表行的處理:2013-08-08