gpt4 book ai didi

javascript - worker.onmessage 上的事件对象是否包含除数据之外的任何有趣内容?

转载 作者:行者123 更新时间:2023-12-03 18:39:42 29 4
gpt4 key购买 nike

当添加回调以捕获通过 web worker 管道传输的数据时,您通常会这样做:

worker.addEventListener("message", function(event) {
var data = event.data;
// Do something with data
...
});

我从未触及任何其他属性,当我将事件对象记录到控制台时,我没有注意到任何有趣的事情。自创建以来Event object 为消息传输增加了一些开销我问:

问: Event 有什么有趣的地方吗? webworker 回调中的对象,还是我们总是只使用 event.data

最佳答案

本例中的事件对象遵循 MessageEvents接口(interface),它包含以下属性:

The data attribute must return the value it was initialized to. When the object is created, this attribute must be initialized to null. It represents the message being sent.

The origin attribute must return the value it was initialized to. When the object is created, this attribute must be initialized to the empty string. It represents, in server-sent events and cross-document messaging, the origin of the document that sent the message (typically the scheme, hostname, and port of the document, but not its path or fragment identifier).

The lastEventId attribute must return the value it was initialized to. When the object is created, this attribute must be initialized to the empty string. It represents, in server-sent events, the last event ID string of the event source.

The source attribute must return the value it was initialized to. When the object is created, this attribute must be initialized to null. It represents, in cross-document messaging, the WindowProxy of the browsing context of the Window object from which the message came.

The ports attribute must return the value it was initialized to. When the object is created, this attribute must be initialized to null. It represents, in cross-document messaging and channel messaging the MessagePort array being sent, if any.

有关详细信息,请参阅规范。

关于javascript - worker.onmessage 上的事件对象是否包含除数据之外的任何有趣内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026125/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com