Any standard mechanism for detecting if a JavaScript is executing as a WebWorker?

后端 未结 5 1686
遇见更好的自我
遇见更好的自我 2020-12-14 19:01

A WebWorker executes with a scope completely separate from the \'window\' context of traditional JavaScript. Is there a standard way for a script to determine if it is, its

5条回答
  •  不知归路
    2020-12-14 19:18

    The spec says:

    The DOM APIs (Node objects, Document objects, etc) are not available to workers in this version of this specification.

    This suggests checking for the absence of document is a good way to check you're in a worker. Alternatively you could try checking for the presence of WorkerGlobalScope?

提交回复
热议问题