标签:listener vat creat instance == target his private data
export default class NotificationCenter {
private eventTarget: cc.EventTarget = new cc.EventTarget();
private static instance: NotificationCenter = null;
public static get Instance(): NotificationCenter {
if (this.instance == null) {
this.instance = new NotificationCenter();
}
return this.instance;
}
/**
* Listen to a notification
* @param name
* @param callback
*/
public on<T>(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
this.eventTarget.on(type, callback, target);
}
/**
* Dispatch a notification
* @param name
*/
public dispatch<T>(type: string, data?: T) {
this.eventTarget.emit(type, type, data);
}
public hasEventListener($type): boolean {
return this.eventTarget != null && this.eventTarget.hasEventListener($type);
}
/**
* Cancel listen
* @param name
*/
public off<T>(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
this.eventTarget.off(type, callback, target);
}
}
标签:listener vat creat instance == target his private data
原文地址:https://blog.51cto.com/aonaufly/2505763