//發(fā)布 emit 訂閱 on {失戀:['哭','吃','購(gòu)物']}
//一對(duì)多 多對(duì)一的關(guān)系
function Girl() { //創(chuàng)建對(duì)象
this._events = {} //對(duì)象的事件集合
} // {失戀:[cry]}
Girl.prototype.on = function (eventName, callback) { //訂閱
if (this._events[eventName]) { //判斷是不是第一次--不是第一次
this._events[eventName].push(callback); //{失戀:[cry,eat,shopping]}
} else {
this._events[eventName] = [callback] //{失戀:[cry]
}
};
Girl.prototype.emit = function (eventName, ...args) { //發(fā)布
if (this._events[eventName]) {
this._events[eventName].forEach(cb => cb.apply(this, args));
// forEach(cb=>cb(...args));
}
};
let girl = new Girl();
let cry = (who) => {
console.log(who + '哭')
};
let eat = (who) => {
console.log(who + '吃')
};
let shopping = (who) => {
console.log(who + '購(gòu)物')
};
girl.on('失戀', cry); // {失戀:[cry]
girl.on('失戀', eat); //{失戀:[cry,eat]
girl.on('失戀', shopping); //{失戀:[cry,eat,shopping]
girl.emit('失戀', '我', '你');
發(fā)布訂閱(第2天)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- When understanding arises, the mind is strong.There is no...
- 09 酒店 Brian拿著兩張房卡,把其中一張遞給少年,隨機(jī)轉(zhuǎn)身把自己套間的房門打開。誰(shuí)料,迷迷糊糊的Justin...