消息事件接口,这一章其实是对后续章节的一个总括介绍,因为后续章节,基本上都需要使用本章节所介绍的接口—MessageEvent。简而言之,无论是跨文档通信,通道通信,服务推送还是WebSocket都会用到MessageEvent,触发message事件的执行。
[Constructor(DOMString type, optional MessageEventInit eventInitDict), Exposed=(Window,Worker)] interface MessageEvent : Event { readonly attribute any data; readonly attribute USVString origin; readonly attribute DOMString lastEventId; readonly attribute MessageEventSource? source; readonly attribute FrozenArray<MessagePort> ports; void initMessageEvent(DOMString type, boolean bubbles, boolean cancelable, any data, USVString origin, DOMString lastEventId, MessageEventSource? source, sequence<MessagePort> ports); }; dictionary MessageEventInit : EventInit { any data = null; USVString origin = ""; DOMString lastEventId = ""; MessageEventSource? source = null; sequence<MessagePort> ports = []; }; typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource;
MessageEvent继承于DOM Event API且属性共享,但是,通信事件没有冒泡而且不能取消,同时也没有默认行为。