JavaScript 事件對象的實(shí)現(xiàn)
更新時(shí)間:2009年07月13日 00:38:27 作者:
前我寫過一篇關(guān)于JavaScript如何實(shí)現(xiàn)面向?qū)ο缶幊痰奈恼隆=裉?,我寫這篇文章跟大家討論一下,如何實(shí)現(xiàn)事件。
比如,我們定義了一個(gè)Classroom對象,這里我們定一個(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)文章
滾動條的監(jiān)聽與內(nèi)容隨著滾動條動態(tài)加載的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獫L動條的監(jiān)聽與內(nèi)容隨著滾動條動態(tài)加載的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02淺談es6 javascript的map數(shù)據(jù)結(jié)構(gòu)
本篇文章主要介紹了淺談es6 javascript的map數(shù)據(jù)結(jié)構(gòu),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12JavaScript中的方法調(diào)用詳細(xì)介紹
這篇文章主要介紹了JavaScript中的方法調(diào)用詳細(xì)介紹,JavaScript中,如果function屬于一個(gè)對象,那么通過對象來訪問該function的行為稱之為“方法調(diào)用”,需要的朋友可以參考下2014-12-12js下通過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)移動端返回上一頁
本文主要介紹了document.referrer實(shí)現(xiàn)移動端返回上一頁的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02[JSF]使用DataModel處理表行事件的實(shí)例代碼
在使用JSF中,最常用的恐怕就要屬于表格的處理了。使用DataModel可以方便地進(jìn)行對表行的處理:2013-08-08