JavaScript 事件對(duì)象的實(shí)現(xiàn)
更新時(shí)間:2009年07月13日 00:38:27 作者:
前我寫過一篇關(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)文章
Javascript計(jì)算時(shí)間差的函數(shù)分享
獲得時(shí)間差,時(shí)間格式為 年-月-日 小時(shí):分鐘:秒 或者 年/月/日 小時(shí):分鐘:秒2011-07-07
在iFrame子頁面里實(shí)現(xiàn)模態(tài)框的方法
今天小編就為大家分享一篇在iFrame子頁面里實(shí)現(xiàn)模態(tài)框的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Bootstrap顯示與隱藏簡單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了bootstrap顯示與隱藏的簡單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
詳解使用fetch發(fā)送post請(qǐng)求時(shí)的參數(shù)處理
這篇文章主要介紹了詳解使用fetch發(fā)送post請(qǐng)求時(shí)的參數(shù)處理的相關(guān)資料,需要的朋友可以參考下2017-04-04
JavaScript eval()函數(shù)定義及使用方法詳解
這篇文章主要介紹了JavaScript eval()函數(shù)定義及使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
本地圖片預(yù)覽(支持IE6/IE7/IE8/Firefox3)經(jīng)驗(yàn)總結(jié)
遇到的本地圖片預(yù)覽的需求,IE6下可以直接從file的value獲取圖片路徑來顯示預(yù)覽,IE7和IE8下通過select獲取file的圖片路徑,再用濾鏡來顯示預(yù)覽,至于FireFox祥看本文吧,希望可以幫助到你2013-03-03

