JavaScript 事件對象的實現(xiàn)
更新時間:2009年07月13日 00:38:27 作者:
前我寫過一篇關(guān)于JavaScript如何實現(xiàn)面向?qū)ο缶幊痰奈恼?。今天,我寫這篇文章跟大家討論一下,如何實現(xiàn)事件。
比如,我們定義了一個Classroom對象,這里我們定一個事件,當(dāng)教室里的人增加超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);
復(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)文章
在iFrame子頁面里實現(xiàn)模態(tài)框的方法
今天小編就為大家分享一篇在iFrame子頁面里實現(xiàn)模態(tài)框的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
詳解使用fetch發(fā)送post請求時的參數(shù)處理
這篇文章主要介紹了詳解使用fetch發(fā)送post請求時的參數(shù)處理的相關(guān)資料,需要的朋友可以參考下2017-04-04
JavaScript eval()函數(shù)定義及使用方法詳解
這篇文章主要介紹了JavaScript eval()函數(shù)定義及使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
本地圖片預(yù)覽(支持IE6/IE7/IE8/Firefox3)經(jīng)驗總結(jié)
遇到的本地圖片預(yù)覽的需求,IE6下可以直接從file的value獲取圖片路徑來顯示預(yù)覽,IE7和IE8下通過select獲取file的圖片路徑,再用濾鏡來顯示預(yù)覽,至于FireFox祥看本文吧,希望可以幫助到你2013-03-03

